all Python 3typesterm
types.TracebackType
interpreter objects
The type of an exception traceback: a chain of frames from where it was caught to where it was raised. Available as exc.__traceback__.
Syntax
exc.__traceback__
The chain goes through tb_next, and the failing line belongs to the last link, not the first: the first tb_lineno points at the line inside try. A saved exception keeps its traceback, and that keeps every frame with its local variables — which is why Python deletes the name from except ... as e at the end of the block. The traceback module produces ready text; walking the chain by hand is rarely needed.
Examples
import types
def inner():
return 1 / 0
try:
inner()
except ZeroDivisionError as e:
tb = e.__traceback__
print(isinstance(tb, types.TracebackType)) # → True
while tb.tb_next:
tb = tb.tb_next
print(tb.tb_frame.f_code.co_name) # → inner
See also
Open in the full glossarydocs.python.orgReport an inaccuracy