Best Way to Handle All Exceptions

Vilya Harvey vilya.harvey at gmail.com
Mon Jul 13 19:55:50 CEST 2009


2009/7/13 seldan24 <seldan24 at gmail.com>:
> Thank you both for your input.  I want to make sure I get started on
> the right track.  For this particular script, I should have included
> that I would take the exception contents, and pass those to the
> logging module.  For this particular script, all exceptions are fatal
> and I would want them to be.  I just wanted a way to catch them and
> log them prior to program termination.

The logging module has a function specifically to handle this case:

try:
    # do something
except:
    logging.exception("Uh oh...")

The exception() method will automatically add details of the exception
to the log message it creates; as per the docs, you should only call
it from within an exception handler.

Hope that helps,

Vil.



More information about the Python-list mailing list