Python Glossary Русский

all Python 3tracebackterm

traceback

module

Prints and extracts stack tracebacks: the same error text the interpreter shows, but under your control.

Syntax

import traceback

Use traceback when an exception has been caught but its full trace must be kept: written to a log, shown in a report, sent to a developer. format_exc() returns a string, print_exc() prints to stderr, and TracebackException stores a snapshot without references to frames. Programs that use logging usually only need logger.exception(), which adds the traceback itself; traceback is for when you need the text separately.

Examples

import traceback
try:
    1 / 0
except ZeroDivisionError:
    text = traceback.format_exc()
print(text.splitlines()[0])  # → Traceback (most recent call last):
print(text.splitlines()[-1])  # → ZeroDivisionError: division by zero

See also

Open in the full glossarydocs.python.orgReport an inaccuracy