Skip to main content

LOGO Release Python Versions License Downloads

exceptionx

English | 中文

exceptionx is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.

The predecessor of exceptionx is gqylpy-exception.

pip3 install exceptionx

Dynamically Creating Exceptions

With exceptionx, you can instantly create exception classes when needed, without the need for advance definition. For example, if you want to throw an exception named NotUnderstandError, you can simply import the library and call it as follows:

import exceptionx as ex

raise ex.NotUnderstandError(...)

Here, NotUnderstandError is not predefined by exceptionx but is dynamically created through the magic method __getattr__ when you try to access e.NotUnderstandError. This flexibility means you can create exception classes with any name as needed.

Additionally, exceptionx ensures that the same exception class is not created repeatedly. All created exception classes are stored in the e.__history__ dictionary for quick access later.

There is another usage, import and create immediately:

from exceptionx import NotUnderstandError

raise NotUnderstandError(...)

Powerful Exception Handling Capabilities

exceptionx also provides a series of powerful exception handling tools:

  • TryExcept: A decorator that catches exceptions raised in the decorated function and outputs the exception information to the terminal (instead of throwing it). This helps prevent the program from crashing due to unhandled exceptions.
  • Retry: A decorator that works similarly to TryExcept but attempts to re-execute the function, controlling the number of attempts and the interval between each retry through parameters. It throws an exception after reaching the maximum number of attempts.
  • TryContext: A context manager that allows you to easily catch exceptions raised in a code block using the with statement and output the exception information to the terminal.

Handling Exceptions in Functions with TryExcept

from exceptionx import TryExcept

@TryExcept(ValueError)
def func():
    int('a')

The default handling scheme is to output brief exception information to the terminal without interrupting program execution. Of course, it can also be output to logs or processed in other ways through parameters.

According to Python programming conventions, exception types should be explicitly specified when handling exceptions. Therefore, when using the TryExcept decorator, it is necessary to explicitly pass the handled exception types.

Retrying Exceptions in Functions with Retry

from exceptionx import Retry

@Retry(sleep=1, count=3)
def func():
    int('a')

If an exception is raised in the decorated function, it will attempt to re-execute the decorated function. The default behavior is to retry exceptions of type Exception and all its subclasses. Calling Retry(sleep=1, count=3) as above means a maximum of 3 attempts will be made, with a 1-second interval between each attempt.

Retry can be used in combination with TryExcept to retry exceptions first and then handle them if the retries are unsuccessful:

from exceptionx import TryExcept, Retry

@TryExcept(ValueError)
@Retry(sleep=1, count=3)
def func():
    int('a')

Handling Exceptions in Contexts with TryContext

from exceptionx import TryContext

with TryContext(ValueError):
    int('a')

With exceptionx, you can handle exceptions in Python programs more flexibly and efficiently, enhancing the robustness and reliability of your code.

Metadata

Release files for exceptionx 4.1.8

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

Source distribution (sdist)

Source distribution for exceptionx 4.1.8
File Size Uploaded Provenance
exceptionx-4.1.8.tar.gz 13.4 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 26.0 kB

Release files / exceptionx-4.1.8.tar.gz

Download URL exceptionx-4.1.8.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c46de43bf66df846bc498ac78f38e94798e54d34037b9aeece84d0d3ca60779e
BLAKE2b-256 checksum
How to use checksums
32210da3cbe5e5e61681e7e3eef98f20eb1999a33df55b5baedefcfe33b45ce2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / exceptionx-4.1.8-py3-none-any.whl

Download URL exceptionx-4.1.8-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1cb4b8c168c93360f0bf33800c0c74bb5257a470028f3b6c4bd494d432d438f
BLAKE2b-256 checksum
How to use checksums
54ddbd97a73f7201784022742b3af860a197d306aa10c5a72a83721b30bb3be8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Security for exceptionx 4.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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