Best way to handle exceptions with try/finally

Zameer perlefar at gmail.com
Wed May 24 16:31:14 CEST 2006


Doing cleaup in except is not the Python way. This is what finally is
for. Using except you would have to at least say:

try:
    stuff()
    cleanup()
except:
    cleanup()
    raise

Duplicate code - not nice. finally is the Python way:

try:
    stuff()
finally:
    cleanup()

That's it. But the return statement should not be a part of finally if
you want exceptions to propagate out of the function containing
try/finally. As mentioned multiple times in the thread.




More information about the Python-list mailing list