libb.catch_exception

catch_exception(f=None, *, level=10)[source]

Decorator that catches and reports exceptions without re-raising.

Can be used with or without parameters to specify the logging level.

Parameters:
  • f (Callable[[ParamSpec(P)], TypeVar(R)] | None) – Function to wrap with exception handling (when used without parameters).

  • level (int) – Logging level for exception details (default: logging.DEBUG).

Return type:

Callable[[Callable[[ParamSpec(P)], TypeVar(R)]], Callable[[ParamSpec(P)], TypeVar(R)]] | Callable[[ParamSpec(P)], Optional[TypeVar(R)]]

Returns:

Wrapped function that prints exceptions instead of raising them.

Default Usage (DEBUG level):

>>> @catch_exception
... def divide(x, y):
...     return x / y
>>> divide(1, 0) is None
True

Specifying Log Level:

>>> @catch_exception(level=logging.ERROR)
... def risky_operation():
...     raise ValueError("Something went wrong")
>>> risky_operation() is None
True