How to get inner exception traceback

bockman at virgilio.it bockman at virgilio.it
Thu Apr 24 10:41:35 EDT 2008


On 24 Apr, 15:00, Christian Heimes <li... at cheimes.de> wrote:
> bock... at virgilio.it 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.



More information about the Python-list mailing list