Python Glossary Русский

all Python 3tracebackfunction

traceback.print_exc()

printing tracebacks

Prints the exception currently being handled, with its traceback, to stderr — and the program keeps running.

Syntax

traceback.print_exc(limit=None, file=None, chain=True)

This is how you catch an error, show it in full and carry on — unlike print(e), which shows only the message without where it happened. It writes to sys.stderr: with python app.py > out.txt the traceback stays on the console, not in the file; file= changes the stream. For logging use logger.exception(), for a string format_exc().

Examples

import sys, traceback
try:
    1 / 0
except ZeroDivisionError:
    traceback.print_exc(file=sys.stdout)  # → ? трассировка с путём к файлу
print('работаем дальше')  # → работаем дальше

See also

Open in the full glossarydocs.python.orgReport an inaccuracy