[Python-Dev] the current behavior of try: ... finally:
Michele Simionato
michele.simionato at gmail.com
Fri May 13 05:58:04 CEST 2005
All this talk about try: ... finally: and exceptions reminded me of a curious
behavior I discovered a while back, i.e. that finally can swallow
your exceptions. This is a contrived example, but shows the point:
def divide1(n1, n2):
try:
result = n1/n2
finally:
print "cleanup"
result = "Infinity\n"
return result # the exception is swallowed away
def divide2(n1, n2):
try:
result = n1/n2
finally:
print "cleanup"
result = "Infinity\n"
return result # the exception is NOT swallowed away
print divide1(10, 0) # no-exception
print divide2(10, 0) # error
If there is an indentation error in "divide2" and the return line is too
indented the exceptions get swallowed by the finally clause.
I am not sure if this is good or bad, but sure it surprised me that a
finally clause could hide my exception.
Michele Simionato
More information about the Python-Dev
mailing list