2

It seems like the traceback returned sys.exc_info() contains only the frames from inside the try: block where you caught the exception from. Is there a way to get the full traceback?

To clarify, I want to get the traceback so that traceback.print_exc() prints the same, full traceback every time it is called in this snippet (just as the last call and the sys.excepthook printed in the end):

def a():
  try:
    raise ValueError
  except:
    traceback.print_exc()
    raise

def b():
  try:
    a()
  except:
    traceback.print_exc()
    raise

try:
  b()
except:
  traceback.print_exc()
  raise

The output for this snippet is:

Traceback (most recent call last):
  File "test.py", line 403, in a
    raise ValueError
ValueError
Traceback (most recent call last):
  File "test.py", line 410, in b
    a()
  File "test.py", line 403, in a
    raise ValueError
ValueError
Traceback (most recent call last):
  File "test.py", line 417, in <module>
    b()
  File "test.py", line 410, in b
    a()
  File "test.py", line 403, in a
    raise ValueError
ValueError
Traceback (most recent call last):
  File "test.py", line 417, in <module>
    b()
  File "test.py", line 410, in b
    a()
  File "test.py", line 403, in a
    raise ValueError
ValueError
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • The best solution I'm aware of is to combine the results of `traceback.extract_tb` and `traceback.extract_stack`. – user2357112 Apr 15 '18 at 22:10
  • 2
    Possible duplicate of [How to get a complete exception stack trace in Python](https://stackoverflow.com/questions/6086976/how-to-get-a-complete-exception-stack-trace-in-python) – metatoaster Apr 15 '18 at 22:26

0 Answers0