Python Encapsulate exception handler decorator - Simple books
from datetime import datetime
import traceback
from functools import wraps
# Abnormal output
def except_output(msg=' abnormal '):
# msg Tips for custom functions
def except_execute(func):
@wraps(func)
def execept_print(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
sign = '=' * 60 + '\n'
print(f'{sign}>>> Abnormal time :\t{datetime.now()}\n>>> Exception function :\t{func.__name__}\n>>>{msg}:\t{e}')
print(f'{sign}{traceback.format_exc()}{sign}')
return execept_print
return except_execute
@except_output(' Abnormal information ')
def lig(a = 5,b = 0):
print(a/b)
lig()