traceback – Traceback Module
This module provides a standard interface to print stack traces of programs. This is useful when you want to print stack traces under program control.
This module implements a subset of the corresponding CPython module,
as described below. For more information, refer to the original
CPython documentation: cpython:traceback.
Available on these boards
- traceback.format_exception(exc: BaseException | Type[BaseException], /, value: BaseException | None = None, tb: types.TracebackType | None = None, limit: int | None = None, chain: bool | None = True) List[str]
Format a stack trace and the exception information.
If the exception value is passed in
exc, then this exception value and its associated traceback are used. This is compatible with CPython 3.10 and newer.If the exception value is passed in
value, then any value passed in forexcis ignored.valueis used as the exception value and the traceback in thetbargument is used. In this case, iftbis None, no traceback will be shown. This is compatible with CPython 3.5 and newer.The arguments have the same meaning as the corresponding arguments to print_exception(). The return value is a list of strings, each ending in a newline and some containing internal newlines. When these lines are concatenated and printed, exactly the same text is printed as does print_exception().
- Parameters:
exc – The exception. Must be an instance of
BaseException. Unused if value is specified.value – If specified, is used in place of
exc.tb (TracebackType) – When value is alsp specified,
tbis used in place of the exception’s own traceback. IfNone, the traceback will not be printed.limit (int) – Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive. Otherwise, print the last
abs(limit)entries. If limit is omitted or None, all entries are printed.chain (bool) – If
Truethen chained exceptions will be printed.
- traceback.print_exception(exc: BaseException | Type[BaseException], /, value: BaseException | None = None, tb: types.TracebackType | None = None, limit: int | None = None, file: io.FileIO | None = None, chain: bool | None = True) None
Prints exception information and stack trace entries.
If the exception value is passed in
exc, then this exception value and its associated traceback are used. This is compatible with CPython 3.10 and newer.If the exception value is passed in
value, then any value passed in forexcis ignored.valueis used as the exception value and the traceback in thetbargument is used. In this case, iftbis None, no traceback will be shown. This is compatible with CPython 3.5 and newer.- Parameters:
exc – The exception. Must be an instance of
BaseException. Unused if value is specified.value – If specified, is used in place of
exc.tb – When value is alsp specified,
tbis used in place of the exception’s own traceback. IfNone, the traceback will not be printed.limit (int) – Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive. Otherwise, print the last
abs(limit)entries. If limit is omitted or None, all entries are printed.file (io.FileIO) – If file is omitted or
None, the output goes tosys.stderr; otherwise it should be an open file or file-like object to receive the output.chain (bool) – If
Truethen chained exceptions will be printed.