How to get inner exception traceback

bockman at bockman at
Thu Apr 24 14:50:51 CEST 2008

On 24 Apr, 13:20, Thomas Guettler <h... at> 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,
> 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

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


More information about the Python-list mailing list