How to get inner exception traceback

Christian Heimes lists at cheimes.de
Thu Apr 24 09:00:06 EDT 2008


bockman at virgilio.it schrieb:
> On 24 Apr, 13:20, Thomas Guettler <h... at tbz-pariv.de> 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/sys.html#sys.exc_info

Christian




More information about the Python-list mailing list