Weird exception handling behavior -- late evaluation in except clause
Roy Smith
roy at panix.com
Sun Dec 2 12:25:22 EST 2012
This is kind of weird (Python 2.7.3):
try:
print "hello"
except foo:
print "foo"
prints "hello". The problem (IMHO) is that apparently the except clause
doesn't get evaluated until after some exception is caught. Which means
it never notices that foo is not defined until it's too late.
This just came up in some code, where I was trying to catch a very rare
exception. When the exception finally happened, I discovered that I had
a typo in the except clause (I had mis-spelled the name of the
exception). So, instead of getting some useful information, I got an
AttributeError :-(
Is this a bug, or intended behavior? It seems to me it would be much
more useful (if slightly more expensive) to evaluate the names of the
exceptions in the expect clause before running the try block.
More information about the Python-list
mailing list