How do I rethrow an exception that contains information about an original exception?

Although this is an old post, there is a much more simple answer to the original question. To rethrow an exception after catching it, just use "raise" with no arguments. The original stack trace will be preserved.


I hope I got the question right.

I'm not sure about Python 2.2 specifics, but this says you can handle exceptions the same way it's done in more recent versions:

try:
    do_stuff()
except ErrorToCatch, e:
    raise ExceptionToThrow(e)

Or maybe the last line should be raise ExceptionToThrow(str(e)). That depends on how your exception is defined. Example:

try:
    raise TypeError('foo')
except TypeError, t:
    raise ValueError(t)

This raises ValueError('foo').

Hope it helps :)


The idiom

try:
   ...
except SomeException:
   ...
   raise

mentioned by @normaldotcom rethrows the error that has been catched as-is, without any modification. It does not answer to the OP, "How do I create a new exception that contain information about an exception that has been catched".

Indeed in some situations, one would like to create a new exception, typically to regroup many possible sources of internal errors into a single exception with a clearer message, while still keeping the traceback to the original error to enable debugging.

A way to achieve this is via the with_traceback method of BaseException. For example,

import sys

try:
  raise ValueError('internal error message')
except ValueError:
  tb = sys.exc_info()[2]
  raise Exception('new error message').with_traceback(tb)