Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to get inner exception traceback

Reply
Thread Tools

How to get inner exception traceback

 
 
Thomas Guettler
Guest
Posts: n/a
 
      04-24-2008
Hi,

How can you get the traceback of the inner exception?

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.

Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-24-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
 
Reply With Quote
 
 
 
 
bockman@virgilio.it
Guest
Posts: n/a
 
      04-24-2008
On 24 Apr, 13:20, Thomas Guettler <(E-Mail Removed)> wrote:
> Hi,
>
> How can you get the traceback of the inner exception?
>
> 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.
>
> * Thomas
>
> --
> Thomas Guettler,http://www.thomas-guettler.de/
> E-Mail: guettli (*) thomas-guettler + de


I'm not sure it ill work since sys.exc_info() might not return a deep
copy of the traceback info,
but you could try to store the inner exception and its traceback as
attributes of the outer exception:

class ReraisedException(Exception):
def __init__(self, message, exc_info):
Exception.__init__(self, message)
self.inner_exception = exc_info

try:
try:
import does_not_exit
except ImportError:
raise ReraisedException("Something wrong", sys.exc_info() )
except ReraisedException, e:
... # here you can use e.inner_exception
except:
...


Ciao
-----
FB
 
Reply With Quote
 
Christian Heimes
Guest
Posts: n/a
 
      04-24-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> On 24 Apr, 13:20, Thomas Guettler <(E-Mail Removed)> wrote:
>> Hi,
>>
>> How can you get the traceback of the inner exception?
>>
>> 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.
>>
>> Thomas
>>
>> --
>> Thomas Guettler,http://www.thomas-guettler.de/
>> E-Mail: guettli (*) thomas-guettler + de

>
> I'm not sure it ill work since sys.exc_info() might not return a deep
> copy of the traceback info,
> but you could try to store the inner exception and its traceback as
> attributes of the outer exception:
>
> class ReraisedException(Exception):
> def __init__(self, message, exc_info):
> Exception.__init__(self, message)
> self.inner_exception = exc_info
>
> try:
> try:
> import does_not_exit
> except ImportError:
> raise ReraisedException("Something wrong", sys.exc_info() )
> except ReraisedException, e:
> ... # here you can use e.inner_exception
> except:


This may lead to reference cycles, please read
http://docs.python.org/dev/library/s...l#sys.exc_info

Christian

 
Reply With Quote
 
bockman@virgilio.it
Guest
Posts: n/a
 
      04-24-2008
On 24 Apr, 15:00, Christian Heimes <(E-Mail Removed)> wrote:
> (E-Mail Removed) schrieb:
>

:
>
> > class ReraisedException(Exception):
> > * * def __init__(self, message, exc_info):
> > * * * * Exception.__init__(self, message)
> > * * * * self.inner_exception = exc_info

>
> > *try:
> > * * * try:
> > * * * * * import does_not_exit
> > * * * except ImportError:
> > * * * * * *raise ReraisedException("Something wrong", sys.exc_info() )
> > *except ReraisedException, e:
> > * * *... # here you can use e.inner_exception
> > *except:

>
> This may lead to reference cycles, please readhttp://docs.python.org/dev/library/sys.html#sys.exc_info
>
> Christian- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -


Thanks. I was not aware of that (Last time I read that section, the
warning was not there).
I usually do something like that in my scripts:

try:
do_something()
except:
err, detail, tb = sys.exc_info()
print err, detail
traceback.print_tb(tb)

According to the document you linked to, also this causes circular
reference, although in my case
it is ininfluent , since I usually do it only before exiting a program
after a
fatal error.

However, this seems like a dark spot in the implementation of
CPython.
Do you now if this has/will be cleaned in Python 3.x ? I'd like to
see a 'print_tb'
method in the exception class, so that I could do something like this:

try:
do_something()
except Exception, e : # I know, in python 3.0 the syntax will be
different
print e
e.print_tb()


Ciao
-------
F.B.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-25-2008
En Thu, 24 Apr 2008 08:20:29 -0300, Thomas Guettler <(E-Mail Removed)>
escribió:

> How can you get the traceback of the inner exception?
>
> 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 already got a couple ways to do it - but I'd ask why do you want to
mask the original exception? If you don't have anything valuable to do
with it, just don't catch it. Or raise the *same* exception+context,
perhaps after modifying it a bit:

try:
try:
import does_not_exist
except ImportError, e:
e.args = ("Something is wrong with the plugin system\nDetails: %s" %
e.args,)
raise # <-- note the "bare" raise
except:
import traceback
print "An error has occurred"
print sys.exc_info()[1]
print sys.exc_info()[0].__name__
traceback.print_tb(sys.exc_info()[2])
# or whatever you want to do with the exception

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments