all Python 3tracebackfunction
traceback.print_last()
printing tracebacks
Prints the last uncaught exception of an interactive session (sys.last_exc) — useful in the REPL when the traceback has scrolled away.
Syntax
traceback.print_last(limit=None, file=None, chain=True)
It only works in the interactive interpreter: sys.last_exc (before 3.12 sys.last_type, last_value, last_traceback) is set when an exception reaches the top level of the REPL. In a script you never get there: an uncaught exception ends the program, a caught one is not recorded, and print_last() raises ValueError('no last exception'). For a caught exception use print_exc().
Examples
import traceback
try:
traceback.print_last()
except ValueError as e:
print(e) # → no last exception
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy