hbutils.reflection.exception
Exception and traceback utilities for debugging and error reporting.
This module provides lightweight helpers for printing and capturing full exception tracebacks. It is designed to simplify error logging and diagnostics by exposing two public utilities:
print_traceback()- Print a full traceback for a given exception.str_traceback()- Capture a full traceback as a string.
Example:
>>> from hbutils.reflection.exception import print_traceback, str_traceback
>>> try:
... raise RuntimeError("runtime error")
... except RuntimeError as e:
... print_traceback(e)
... text = str_traceback(e)
... assert "RuntimeError" in text
__all__
- hbutils.reflection.exception.__all__ = ['print_traceback', 'str_traceback']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
print_traceback
- hbutils.reflection.exception.print_traceback(err: BaseException, file: TextIO | None = None) None[source]
Print the full traceback for an exception object.
This function delegates to
traceback.print_exception()and prints the complete traceback of the provided exception, including chained exceptions if present.- Parameters:
err (BaseException) – Exception object to print a traceback for.
file (TextIO or None, optional) – Output file-like object. If
None, defaults to standard output.
- Returns:
This function does not return a value.
- Return type:
None
Example:
>>> try: ... raise RuntimeError('runtime error') ... except RuntimeError as e: ... print_traceback(e) Traceback (most recent call last): File "<stdin>", line 2, in <module> raise RuntimeError('runtime error') RuntimeError: runtime error
Note
See
str_traceback()for capturing the traceback as a string.
str_traceback
- hbutils.reflection.exception.str_traceback(err: BaseException) str[source]
Get the full traceback for an exception object as a string.
This function captures the output of
print_traceback()into an in-memory buffer and returns it as a string.- Parameters:
err (BaseException) – Exception object to extract traceback from.
- Returns:
Full string representation of the traceback.
- Return type:
str
Example:
>>> try: ... raise RuntimeError('runtime error') ... except RuntimeError as e: ... s = str_traceback(e) ... print(s) Traceback (most recent call last): File "<stdin>", line 2, in <module> raise RuntimeError('runtime error') RuntimeError: runtime error