The `exceptionx` is a flexible and convenient Python exception handling library that allows you to dynamically create exception classes and provides various exception handling mechanisms.
Project description
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 toTryExcept
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 thewith
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file exceptionx-4.1.7.tar.gz
.
File metadata
- Download URL: exceptionx-4.1.7.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc80f145297aeb22a3333c7ec5e9b5ce1f102b29bc6523a6f46f895c186474de |
|
MD5 | ee2f7b27d5bdcc851b12ac6b786a068b |
|
BLAKE2b-256 | 34142e9afa6343ac01203f6bfc270d5a872bc70819f5faf9761731f67635673b |
File details
Details for the file exceptionx-4.1.7-py3-none-any.whl
.
File metadata
- Download URL: exceptionx-4.1.7-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0fdad458fd6213d551bdbbc1c7a03d35fd23b9b7f6500262a3f2eaae799c179 |
|
MD5 | 639ca5bb464628a7a9c6270c290325e8 |
|
BLAKE2b-256 | f4fded23f28a23dc3f78ddd26de3e756c51ba607bbc18c94ad690fc5e544f9df |