Handling exceptions consistently with the logging module.

J

Jeremy Fincher

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top