PyDocs

Nav

Python Runtime Services

format_exception_only

traceback

function (source)
  • format_exception_only(exc, /, [value, ]*, show_group=False)
Format the exception part of a traceback.

The return value is a list of strings, each ending in a newline.

The list contains the exception's message, which is
normally a single string; however, for :exc:`SyntaxError` exceptions, it
contains several lines that (when printed) display detailed information
about where the syntax error occurred. Following the message, the list
contains the exception's ``__notes__``.

When *show_group* is ``True``, and the exception is an instance of
:exc:`BaseExceptionGroup`, the nested exceptions are included as
well, recursively, with indentation relative to their nesting depth.
Changed in 3.10, 3.11, 3.13
  • 3.10: The etype parameter has been renamed to exc and is now positional-only.
  • 3.11: The returned list now includes any notes attached to the exception.
  • 3.13: show_group parameter was added.
1 Example
>>> import traceback
>>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in <module>\n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(an_error)
['IndexError: tuple index out of range\n']
from the Python docs
See Also
No see-alsos for traceback/format_exception_only
0 Notes
No notes for format_exception_only