Stack trace of exceptions



examples/exceptions/stack_trace.py
import traceback

def bar():
    foo()

def foo():
    raise Exception("hi")

def main():
    try:
        bar()
    except Exception as err:
        track = traceback.format_exc()
        print("The caught:\n")
        print(track)

    print("---------------------")
    print("The original:\n")
    bar()


main()

The caught:

Traceback (most recent call last):
  File "stack_trace.py", line 11, in main
    bar()
  File "stack_trace.py", line 4, in bar
    foo()
  File "stack_trace.py", line 7, in foo
    raise Exception("hi")
Exception: hi

---------------------
The original:

Traceback (most recent call last):
  File "stack_trace.py", line 20, in <module>
    main()
  File "stack_trace.py", line 17, in main
    bar()
  File "stack_trace.py", line 4, in bar
    foo()
  File "stack_trace.py", line 7, in foo
    raise Exception("hi")
Exception: hi