Skip to content

Instantly share code, notes, and snippets.

@squito
Last active December 11, 2015 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squito/c385d3ad1932e875c7d7 to your computer and use it in GitHub Desktop.
Save squito/c385d3ad1932e875c7d7 to your computer and use it in GitHub Desktop.
"chaining" python exception
import traceback
import sys
def a(x): b(x)
def b(x): c(x)
def c(x): d(x)
def d(x): raise Exception("blah %s" % x)
try:
a(1)
except:
orig_tb = traceback.format_exception(*sys.exc_info())[1:]
raise Exception(''.join(["foo\n", "\tcaused by:\n"] + orig_tb))
## output:
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
Exception: foo
caused by:
File "<stdin>", line 2, in <module>
File "<stdin>", line 1, in a
File "<stdin>", line 1, in b
File "<stdin>", line 1, in c
File "<stdin>", line 1, in d
Exception: blah 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment