Skip to main content

Backport of PEP 654 (exception groups)

Project description

Build Status Code Coverage

This is a backport of the BaseExceptionGroup and ExceptionGroup classes from Python 3.11.

It contains the following:

  • The exceptiongroup.BaseExceptionGroup and exceptiongroup.ExceptionGroup classes

  • A utility function (exceptiongroup.catch()) for catching exceptions possibly nested in an exception group

  • Patches to the TracebackException class that properly formats exception groups (installed on import)

  • An exception hook that handles formatting of exception groups through TracebackException (installed on import)

If this package is imported on Python 3.11 or later, the built-in implementations of the exception group classes are used instead, TracebackException is not monkey patched and the exception hook won’t be installed.

See the standard library documentation for more information on exception groups.

Catching exceptions

Due to the lack of the except* syntax introduced by PEP 654 in earlier Python versions, you need to use exceptiongroup.catch() to catch exceptions that are potentially nested inside an exception group. This function returns a context manager that calls the given handler for any exceptions matching the sole argument.

The argument to catch() must be a dict (or any Mapping) where each key is either an exception class or an iterable of exception classes. Each value must be a callable that takes a single positional argument which is the exception object to be handled.

Thus, the following Python 3.11+ code:

try:
    ...
except* (ValueError, KeyError) as exc:
    print('Caught exception:', type(exc))
except* RuntimeError:
    print('Caught runtime error')

would be written with this backport like this:

from exceptiongroup import catch

def value_key_err_handler(exc: Exception) -> None:
    print('Caught exception:', type(exc))

def runtime_err_handler(exc: RuntimeError) -> None:
    print('Caught runtime error')

with catch({
    (ValueError, KeyError): value_key_err_handler,
    RuntimeError: runtime_err_handler
}):
    ...

NOTE: Just like with except*, you cannot handle BaseExceptionGroup or ExceptionGroup with catch().

Notes on monkey patching

To make exception groups render properly when an unhandled exception group is being printed out, this package does two things when it is imported on any Python version earlier than 3.11:

  1. The traceback.TracebackException class is monkey patched to store extra information about exception groups (in __init__()) and properly format them (in format())

  2. An exception hook is installed at sys.excepthook, provided that no other hook is already present. This hook causes the exception to be formatted using traceback.TracebackException rather than the built-in rendered.

To prevent the exception hook and patches from being installed, set the environment variable EXCEPTIONGROUP_NO_PATCH to 1.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exceptiongroup-1.0.0rc5.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exceptiongroup-1.0.0rc5-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file exceptiongroup-1.0.0rc5.tar.gz.

File metadata

  • Download URL: exceptiongroup-1.0.0rc5.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for exceptiongroup-1.0.0rc5.tar.gz
Algorithm Hash digest
SHA256 665422550b9653acd46e9cd35d933f28c5158ca4c058c53619cfa112915cd69e
MD5 cbb63cb46a10ecc58f434c1392a410a8
BLAKE2b-256 4011c99356538f598c305c81400a1e9f5ed82f8b1192bcab20ae6125cf7c6888

See more details on using hashes here.

File details

Details for the file exceptiongroup-1.0.0rc5-py3-none-any.whl.

File metadata

File hashes

Hashes for exceptiongroup-1.0.0rc5-py3-none-any.whl
Algorithm Hash digest
SHA256 295a9d7847f9ad08267f47c701a676ec70a64200a360dd49eb513f72209b09f4
MD5 9f54cffca98547d6fd6c149643623f85
BLAKE2b-256 3a2b5573c354dbc27b8fcceddb4d7a7f7e1d63702c9acb528dbfea2d77e43513

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page