title: Python: Exception decorator. How to preserve stacktrace
body: <p>I am writing a decorator to apply to a function.  It should catch any exception, and then raise a custom exception based on the original exception message.  (This is because suds throws a generic WebFault exception, from whose message I parse the exception thrown by the web service and raise a Python exception to mirror it.)</p>
  
  <p>However, when I raise the custom exception in the wrapper, I want the stacktrace to point to the function that raised the original WebFault exception.  What I have so far raises the correct exception (it dynamically parses the message and instantiates the exception class).  <strong>My question:</strong> How can I preserve the stacktrace to point to the original function that raised the WebFault exception?</p>
  
  <pre><code>from functools import wraps
  
  def try_except(fn):
          def wrapped(*args, **kwargs):
              try:
                  fn(*args, **kwargs)
              except Exception, e:
                  parser = exceptions.ExceptionParser()
                  raised_exception = parser.get_raised_exception_class_name(e)
                  exception = getattr(exceptions, raised_exception)
                  raise exception(parser.get_message(e))
          return wraps(fn)(wrapped)
  </code></pre>