优雅地处理python异常
Project description
TryTry
install
pip install trytry
Example
handle exception
from trytry import trytry
@trytry
def my_function():
raise FileNotFoundError('file not found')
@trytry
def my_function2():
print(1 / 0)
@trytry.exception(ZeroDivisionError)
def handle_zero_division_error(func, e):
print(func.__name__, str(e))
@trytry.exception(FileNotFoundError)
def handle_file_not_found_error(func, e):
print(func.__name__, str(e))
if __name__ == '__main__':
my_function()
my_function2()
handle all exception
from trytry import trytry
@trytry
def my_function():
raise FileNotFoundError('file not found')
@trytry
def my_function2():
print(1 / 0)
@trytry.exception(Exception)
def handle_all_error(func, e):
print(func.__name__, str(e))
if __name__ == '__main__':
my_function()
my_function2()
All of the above exceptions are caught and are global exceptions. You can also catch exceptions for specific functions.
from trytry import trytry
@trytry
def my_function():
print(1 / 0)
@my_function.exception(ZeroDivisionError)
def handle_zero_division_error(func, e):
print(func.__name__, str(e))
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
trytry-0.1.2.tar.gz
(2.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trytry-0.1.2.tar.gz.
File metadata
- Download URL: trytry-0.1.2.tar.gz
- Upload date:
- Size: 2.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/22.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e28653d6bce593148f12631103c1fde9cad2cff0a6ce3288ee42fdb83562372
|
|
| MD5 |
b828d1b7ce40eec6493979be2ea803a4
|
|
| BLAKE2b-256 |
65397725b31b7236818074c2ac76e1d613d797119c1717218069f6c68b2db5b5
|
File details
Details for the file trytry-0.1.2-py3-none-any.whl.
File metadata
- Download URL: trytry-0.1.2-py3-none-any.whl
- Upload date:
- Size: 2.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/22.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da0ab7b2c336efca3cff92b8792df38c5c1838609c7ecd84b4ac131c1030fe6e
|
|
| MD5 |
9cbf93ed929b648faa531b390ce40285
|
|
| BLAKE2b-256 |
3e6d059ba5a539b4f2fe03c641478b1f6fafb09f705a63eb31096b596a6b68b2
|