Python: print stack trace after catching exception
examples/python/stack_trace.py
import traceback def g(): f() def f(): raise Exception("hi") try: g() except Exception as e: track = traceback.format_exc() print(track) print("---------------------") g()
The upper stack-trace was printed by the traceback module. The lower one would be if we did not catch it.
$ python examples/python/stack_trace.py Traceback (most recent call last): File "examples/python/stack_trace.py", line 11, in <module> g() File "examples/python/stack_trace.py", line 4, in g f() File "examples/python/stack_trace.py", line 7, in f raise Exception("hi") Exception: hi --------------------- Traceback (most recent call last): File "examples/python/stack_trace.py", line 19, in <module> g() File "examples/python/stack_trace.py", line 4, in g f() File "examples/python/stack_trace.py", line 7, in f raise Exception("hi") Exception: hi
Published on 2019-07-09
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post