Getting double thrown exceptions Original stack trace in python

If I have a scenario in which an exception is thrown, it is caught and then re-raised inside the except: block, is there a way to grab the original frame stack from which it was raised?

The stack trace that prints as python outputs describes the place where the exception occurs the second time. Is there a way to raise the exception so that the frame of the stack from which the exception was thrown is displayed?

+4
source share
1 answer

A common mistake is to re-raise the exception by specifying the instance of the exception again, for example:

except Exception, ex: # do something raise ex 

Discards the initial trace information and starts a new one. Instead, you should do this without explicitly specifying an exception (i.e., use a bare raise ):

 except Exception, ex: # do something raise 

This saves all the raw information in the stack trace. See this section in the docs for a somewhat useful background.

+11
source

All Articles