Skip to main content

exceptlib - exception handling mechanisms


Summary

Exceptions occur in Python when something goes wrong. For example, "abcd"[63] raises IndexError. What to do with an exception is context dependent-- in any langauge, context informs a spectrum of handling mechanisms. At one end, certain exceptions must be fatal, while at the other end a given exception acts as control of flow and is suppressed. A process to arrive somewhere on this spectrum is summarized by Joe Armstrong's let it crash principal, which is also covered in Python's exception documentation:

[...] it is good practice to be as specific as possible with the types of exceptions that we intend to handle, and to allow any unexpected exceptions to propagate on.

This experimental library extends Python's exception handling mechanism with two additional functionalities:

  • handling exceptions by module;
  • and exception class scraping from modules.

This library is under active development; it may require additional tuning and there are plans to add additional features.

Usage notes

The main feature of exceptlib is handling exceptions by module with ExceptionFrom. For example:

import re

from exceptlib import ExceptionFrom


try:
    re_obj = re.compile(7)
except ExceptionFrom(re) as e:
    print(f"re raised {e[0]}")

The malformed call to re.compile raises TypeError. The interpreter calls ExceptionFrom when trying to enter the except block. When called, ExceptionFrom compares its module arguments to those in the current exception's traceback to determine if any are involved. Since re raised, ExceptionFrom returns the tuple (TypeError) and causes that except block to enter.

Handling exceptions by module enables a wider degree of abstraction for exception handling. Specific advantages include:

  • enable certain retries and fallbacks for exceptions in production with less boilerplate;
  • enhance or simplify RCAs in test;
  • and generally widen the scope of except clauses.

In addition to its functionality as the predicate of the except statment, exceptlib.ExceptionFrom provides additional introspective functionality:

...

# a tuple of distinct exceptions raised by the input modules
excs_raised_from: tuple = ExceptionFrom(re, urllib)

Calling ExceptionFrom when there is no current exception will direct it to scrape distinct exceptions from raise statements in the specified modules.

Installation notes

This library is available through GitHub.

Release files for exceptlib 2025.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for exceptlib 2025.1
File Size Uploaded
exceptlib-2025.1.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for exceptlib 2025.1
File Interpreter ABI Platform
exceptlib-2025.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.8 kB

Release files / exceptlib-2025.1.tar.gz

Download URL exceptlib-2025.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
92d0053bcdf368c584dccc72330632c04a60d43b606efc220913428e4f5c9578
BLAKE2b-256 checksum
How to use checksums
a4a1411e47f560d60128a2956defa1f8a1f89a0251dd474c70117f1329126db8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.14.0a0

Release files / exceptlib-2025.1-py3-none-any.whl

Download URL exceptlib-2025.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e4536c986bc5b61d3358b400455a8aa88bc51d565a2265ddf2ace8f9d6e907e
BLAKE2b-256 checksum
How to use checksums
d098de8256e1b66368e39fc9b57eb0e140d3e76c67174b1c1ed6a7b321de2869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.14.0a0

Release history Release notifications | RSS feed

This release

2025.1 This release

2 release files

2024.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page