Python Glossary Русский

all Python 3sysfunction

sys.exc_info()

exceptions

Returns a (type, exception, traceback) tuple for the exception being handled, or (None, None, None) when no handler is running.

Syntax

exc_type, exc_value, exc_tb = sys.exc_info()

This is the old form: since Python 3.11 sys.exception() gives the same thing as one object, and the type and traceback follow from it. An all-None tuple is not an error: calling it outside (or after) except returns three Nones, so unpacking t, v, tb = sys.exc_info() works and t.__name__ fails later with AttributeError. Do not keep the traceback in long-lived objects — it pins frames with all their locals and delays freeing memory.

Examples

# Python 3.11+
import sys
try:
    1 / 0
except ZeroDivisionError:
    t, v, tb = sys.exc_info()
    print(t.__name__, v, tb.tb_lineno)  # → ZeroDivisionError division by zero 4
    print(v is sys.exception())  # → True
print(sys.exc_info())  # → (None, None, None)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy