deco1:
def deco(func):
def wrapper(*args, **kwargs):
f = func(*args, **kwargs)
print("deco success")
return f
return wrapper
deco2:
def deal_exc(func):
def wrapper(*arg, **kwargs):
try:
return func(*arg, **kwargs)
except Exception as e:
print(str(e))
return wrapper
use decos:
@deal_exc
@deco
def a_function():
print(0 / 100)
raise Exception("666")
how to skip print("deco success")
when i raise an exception and let deal_exc
decorator deal with the exception?
When you put a decorator above a decorator, the upper decorator actually wraps(read calls) the lower decorator & similarly, the lower decorator wraps(read calls) the actual function.
So, you cannot skip the handling unless you remove the decorator.
In case you want the inner decorator(deco
) to NOT handle some specific scenario, you'll have to apply checks for it. eg:
In the above case, since there is no try-catch
handling in the inner decorator, the exception thrown by the function will automatically be handled by the outermost decorator(deal_exc
).