Skip to main content

Exception handling mechanisms

Project description

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.

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

exceptlib-2025.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

exceptlib-2025.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file exceptlib-2025.1.tar.gz.

File metadata

  • Download URL: exceptlib-2025.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.14.0a0

File hashes

Hashes for exceptlib-2025.1.tar.gz
Algorithm Hash digest
SHA256 92d0053bcdf368c584dccc72330632c04a60d43b606efc220913428e4f5c9578
MD5 37dffc3038a96da144e29e9472888e2c
BLAKE2b-256 a4a1411e47f560d60128a2956defa1f8a1f89a0251dd474c70117f1329126db8

See more details on using hashes here.

File details

Details for the file exceptlib-2025.1-py3-none-any.whl.

File metadata

  • Download URL: exceptlib-2025.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.14.0a0

File hashes

Hashes for exceptlib-2025.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4536c986bc5b61d3358b400455a8aa88bc51d565a2265ddf2ace8f9d6e907e
MD5 a4651c61b03a6889a564736c33c06ea3
BLAKE2b-256 d098de8256e1b66368e39fc9b57eb0e140d3e76c67174b1c1ed6a7b321de2869

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