Question about exception-handling mechanism
Gordon McMillan
gmcm at hypernet.com
Tue Apr 25 10:06:05 EDT 2000
willfg at my-deja.com wrote
> ...A colleague asked why in an exception handling mechanism
> you'd want the ELSE block to be executed if you don't throw an
> exception as opposed to a FINALLY block. Anyone used this feature
> in practice? Thanks in advance for your input, -- Will
A finally clause is *always* executed, so it's not really in
competition with a try / else. If everything goes fine, these are
equivalent:
try:
do_something()
do_morestuff()
except NoBananasException:
yesweaint()
try:
do_something()
except NoBananasException:
yesweaint()
else:
do_morestuff()
Now we expect do_something() to sometimes throw a specific
exception, and we're prepared to deal with it. But we can't deal
with the same exception coming from do_morestuff(). So the
first snippet may cause a problem by "recovering" from the
wrong error. That's the rationale for try / else, (essentially, we
don't have to use a flag variable).
- Gordon
More information about the Python-list
mailing list