How to get inner exception traceback
Peter Otten
__peter__ at web.de
Thu Apr 24 08:04:27 EDT 2008
Thomas Guettler wrote:
> How can you get the traceback of the inner exception?
You have to record it yourself or it will be lost.
> try:
> try:
> import does_not_exit
> except ImportError:
> raise Exception("something wrong")
> except:
> ...
>
>
> Background: In Django some exceptions are caught and a new
> exception gets raised. Unfortunately the real error is hard
> to find. Sometimes I help myself and change (in this example)
> ImportError to e.g. IOError and then I can see the real root
> of the problem. But maybe there is a way to get the inner
> exception and its traceback. This could be displayed in the
> debug view.
You can get the current exception and traceback with
sys.exc_info()
and later print or format it using the traceback module.
>>> try:
... 1/0
... except Exception:
... x = sys.exc_info()
... raise ValueError
...
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
ValueError
>>> traceback.print_exception(*x)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
Peter
More information about the Python-list
mailing list