ExceptionGroup
Description:
- A backport of the BaseExceptionGroup and ExceptionGroup classes from Python 3.11. Use python 3.11+ type syntax in previous Python versions.
Resources:
- [GitHub] https://github.com/agronholm/exceptiongroup
- [GitHub Example Tests] https://github.com/agronholm/exceptiongroup/blob/main/tests/test_catch.py
Example 1
Section titled “Example 1”Backport of python 3.11+ exception handling
from exceptiongroup import ExceptionGroup, catch
def value_key_err_handler(excgroup: ExceptionGroup) -> None: for exc in excgroup.exceptions: print('Caught exception:', type(exc))
def runtime_err_handler(exc: ExceptionGroup) -> None: print('Caught runtime error')
with catch({ (ValueError, KeyError): value_key_err_handler, RuntimeError: runtime_err_handler}): example_dct = {"hi": "eugo"}
# Generate a key error res = example_dct["not_a_key"]ganymede is made by Eugo