https://github.com/mic1on/trytry
Handle python exceptions gracefully
https://github.com/mic1on/trytry
Last synced: 10 months ago
JSON representation
Handle python exceptions gracefully
- Host: GitHub
- URL: https://github.com/mic1on/trytry
- Owner: mic1on
- Created: 2022-12-26T03:56:45.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-26T07:54:44.000Z (about 3 years ago)
- Last Synced: 2025-01-14T11:14:16.707Z (12 months ago)
- Language: Python
- Size: 3.91 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[中文文档](README_ZH.md)
## install
```bash
pip install trytry
```
## Example
### handle exception
```python
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
```python
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.
```python
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))
```