def try_except(func):
# try-except function. Usage: @try_except decorator
def handler(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return None
return handler
用法:
@try_except
def func(x, y):
return x/y
print(func(1,2))
print(func(2,3))
print(func(1,0))