Created
November 12, 2013 16:03
-
-
Save babo/7433428 to your computer and use it in GitHub Desktop.
how to re-raise an exception with proper stack information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import traceback | |
def f(x): | |
a = x / 0 | |
return a | |
def g(): | |
a = {} | |
print a[42] | |
def h(): | |
try: | |
f(12) | |
except Exception: | |
saved_info = sys.exc_info() | |
print 'Exception of type ', saved_info[0] | |
try: | |
g() | |
except Exception: | |
print 'An exception during exception handling' | |
raise saved_info[0], saved_info[1], saved_info[2] | |
def main(): | |
try: | |
h() | |
except Exception: | |
saved_info = sys.exc_info() | |
print 'in main' | |
traceback.print_tb(saved_info[2]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But if I want something like this:
This is nicer, right?