Capture traceback output for logs or APIs.
Section: Tracebacks and Exceptions
Format exception as a string
python
python
import traceback
try:
risky_call()
except Exception:
error_text = traceback.format_exc()Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Tracebacks and Exceptions
Print the current exception traceback
Dump the active exception traceback in an except block.
Get exception info tuple
Access exception type, value, and traceback explicitly.
Preserve context with raise from
Chain exceptions to keep original failure context.
Enable faulthandler
Print Python tracebacks on fatal errors like segfaults.
Enable faulthandler from CLI
Turn on fatal error tracebacks without code changes.