Understanding python try catch else finally clause behavior -


python 2.6.5 (r265:79063, oct 1 2012, 22:07:21)

i have this:

def f():     try:         print "a"         return     except:         print "b"     else:         print "c"     finally:         print "d" f() 

this gives:

a d  

and not expected

a  c  d  

if comment out return,

a  c  d  

how remember behavior in python?

when in doubt, consult the docs:

the optional else clause executed if , when control flows off end of try clause

currently, control “flows off end” except in case of exception or execution of return, continue, or break statement.

since you're returning body of try block, else not executed.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -