coding style - try, except

Christian Heimes lists at cheimes.de
Wed Feb 25 13:26:23 EST 2009


RGK wrote:
> Any input appreciated :)

How about:

import logging

try:
   # run your function
   some_function()
except Exception:
   # except only the exceptions you *really* want to catch
   # at most you should except "Exception" since it doesn't
   # catch KeyboardInterrupt and SystemExit
   logging.exception("An error has occured")
   # logging is preferred over a simple print because it
   # also prints out a nice traceback
else:
   # the else block is reached when no exception has occured
   some_other()
finally:
   # the finally block is *always* executed at least
   # use it to clean up some resources
   some_cleanup_code()

Christian




More information about the Python-list mailing list