Handling exceptions consistently with the logging module.

Jeremy Fincher tweedgeezer at hotmail.com
Tue Dec 2 07:42:12 EST 2003


I've just converted my application to use the logging module for its
logging needs (it's quite excellent, actually :)).  One thing I still
have to resolve, though: in many places in my application, I want to
catch *all* exceptions *except*, for instance, KeyboardInterrupt and
SystemExit.  So I'll have code like this:

try:
    callback(data)
except Exception, e:
    log.exception("Uncaught exception from callback")
except: # Crazy string exceptions!
    log.exception("Uncaught exception from callback")

With my former hand-rolled logging system, those "log.exception" calls
were actually "debug.recoverableException" calls, and if the exception
debug.recoverableException found was in the list of "deadly"
exceptions, it would simply re-raise it.

I've tried that in my logging subclass like so:

    def formatException(self, (E, e, tb)):
        for exn in deadlyExceptions:
            if issubclass(e.__class__, exn):
                raise
        return cgitb.text((E, e, tb)).rstrip('\r\n')

This, however, doesn't seem to work -- log.exception seems to be
logging (and not re-raising) KeyboardInterrupt, and I have to press
Ctrl-C three times to actually exit my application.

Anyway, I was curious if there's a way to do what I want to do without
modifying every "catch all exceptions" places to catch everything but
the deadly exceptions.

Thanks,
Jeremy




More information about the Python-list mailing list