Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to handle Exception Stack Traces in Python

I am writing some code which can generate Exceptions deep in the stack, and I have a layer nearer the top which catches these exceptions and sends them off for processing by an error handling module. What I would like is for the error handling module to be able to print the stack trace into its error log, but I'm finding it difficult to work out what the way to do this is.

Some notes on the background:

  • The code is multi-threaded, so I'm not certain of the behaviour of sys.last_traceback

  • I have tried capturing the stack in the constructor of the exception itself. sys.last_traceback is sometimes None in this case (since it only exists in the case of unhandled exceptions), and not always correct. I am currently toying with using

    self.stack = traceback.format_stack()[:-2]

in the constructor of the exception, and while this looks promising in terms of information, it doesn't feel like the "right" way to do it

  • All examples of how to do this that I've been able to find show how to print the stack trace in the except block, not in some later error handling module, and this appears to be different in behaviour from what I want. (see, for example, Print current call stack from a method in Python code)

  • I've mined the traceback module in the python docs (http://docs.python.org/library/traceback.html), and haven't been able to figure out whether this even does what I want. It seems mostly concerned with formatting tracebacks as you might retrieve from sys.last_traceback. It has some examples of usage, and none of them line up with what I'm trying to do.

I don't believe that I'm the first person to ever want to do this, so I must be missing something. Any pointers in the right direction much appreciated.

like image 311
Richard J Avatar asked Apr 11 '12 09:04

Richard J


1 Answers

Your first line on handling the exception could be:

exc_type, exc_value, exc_traceback = sys.exc_info()

You can store these variables or pass them any how you like, then later use the traceback module to display them.

like image 140
Ali Afshar Avatar answered Sep 30 '22 12:09

Ali Afshar