Custom Search

Sunday, June 26, 2011

python try finally return

def x1():
    try:
        print " in try block"     
            return 'returned value'
    finally:
                 print "in Finally block"

print"-----------test1----------\n",x1()





def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = 2   
                 print "in Finally block"

print"-----------test2----------\n",x1()





def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = 2   
                 print "in Finally block"
        return a

print"-----------test3----------\n",x1()




def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = a+1   
                 print "in Finally block"

print"-----------test4----------\n",x1()



def x1():
    a= 1
         try:
        a = a+1
        print " in try block"     
                 return  a
         finally:
             a = a+2   
                 print "in Finally block"

print"-----------test5----------\n",x1()




def x1():
    try:
        a= 1
    finally:
        pass
         try:
        a = a+1
        print " in try block"     
                 return  a
         finally:
             a = a+2   
                 print "in Finally block"

print"-----------test6----------\n",x1()


OUTPUT
=========
-----------test1----------
 in try block
in Finally block
returned value
-----------test2----------
 in try block
in Finally block
1
-----------test3----------
 in try block
in Finally block
2
-----------test4----------
 in try block
in Finally block
1
-----------test5----------
 in try block
in Finally block
2
-----------test6----------
 in try block
in Finally block
2

No comments:

Post a Comment