Fix Python – “Inner exception” (with traceback) in Python?


Asked By – EMP

My background is in C# and I’ve just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It’s quite easy in C#, but how do I do it in Python?

Eg. in C# I would do something like this:

catch (Exception ex)
  throw new ApplicationException("Failed to process file " + filePath, ex);

In Python I can do something similar:

except Exception as e:
  raise Exception('Failed to process file ' + filePath, e)

…but this loses the traceback of the inner exception!

Edit: I’d like to see both exception messages and both stack traces and correlate the two. That is, I want to see in the output that exception X occurred here and then exception Y there – same as I would in C#. Is this possible in Python 2.6? Looks like the best I can do so far (based on Glenn Maynard’s answer) is:

except Exception as e:
  raise Exception('Failed to process file' + filePath, e), None, sys.exc_info()[2]

This includes both the messages and both the tracebacks, but it doesn’t show which exception occurred where in the traceback.

Now we will see solution for issue: “Inner exception” (with traceback) in Python?


Python 2

It’s simple; pass the traceback as the third argument to raise.

import sys
class MyException(Exception): pass

    raise TypeError("test")
except TypeError, e:
    raise MyException(), None, sys.exc_info()[2]

Always do this when catching one exception and re-raising another.

This question is answered By – Glenn Maynard

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0