I came across a strange behavior in python. I could not find information about this in the python help or on SE so here it is:
def divide(x, y):
print 'entering divide'
try:
return x/y
except:
print 'error'
else:
print 'no error'
finally:
print 'exit'
print divide(1, 1)
print divide(1, 0)
the output:
entering divide
exit
1
entering divide
error
exit
None
It seems that python will not go inside the else block if a value is returned in the try. However, it will always go in the finally block. I don’t really understand why. Can someone help me with this logic?
thanks
http://docs.python.org/reference/compound_stmts.html#the-try-statement