Error Handling in Python using Decorators Last Updated : 21 Sep, 2021 Comments Improve Suggest changes Like Article Like Report Decorators in Python is one of the most useful concepts supported by Python. It takes functions as arguments and also has a nested function. They extend the functionality of the nested function. Example: Python3 # defining decorator function def decorator_example(func): print("Decorator called") # defining inner decorator function def inner_function(): print("inner function") func() return inner_function # defining outer decorator function @decorator_example def out_function(): print("outer function") out_function() Output: Decorator called inner function outer functionError handling using decorators The following example shows how a general error handling code without use of any decorator looks like: Python3 def mean(a,b): try: print((a+b)/2) except TypeError: print("wrong data types. enter numeric") def square(sq): try: print(sq*sq) except TypeError: print("wrong data types. enter numeric") def divide(l,b): try: print(b/l) except TypeError: print("wrong data types. enter numeric") mean(4,5) square(21) divide(8,4) divide("two","one") Output : 4.5 441 0.5 wrong data types. enter numeric Even though there is nothing logically wrong with the above code but it lacks clarity. To make the code more clean and efficient decorators are used for error handling. The following example depicts how the above code can be more presentable and understandable by use of decorators: Python3 def Error_Handler(func): def Inner_Function(*args, **kwargs): try: func(*args, **kwargs) except TypeError: print(f"{func.__name__} wrong data types. enter numeric") return Inner_Function @Error_Handler def Mean(a,b): print((a+b)/2) @Error_Handler def Square(sq): print(sq*sq) @Error_Handler def Divide(l,b): print(b/l) Mean(4,5) Square(14) Divide(8,4) Square("three") Divide("two","one") Mean("six","five") Output : 4.5 196 0.5 Square wrong data types. enter numeric Divide wrong data types. enter numeric Mean wrong data types. enter numeric Comment More infoAdvertise with us Next Article Error Handling in Python using Decorators R rakshita_iyer Follow Improve Article Tags : Python Python Decorators Practice Tags : python Similar Reads Memoization using decorators in Python Recursion is a programming technique where a function calls itself repeatedly till a termination condition is met. Some of the examples where recursion is used are a calculation of fibonacci series, factorial, etc. But the issue with them is that in the recursion tree, there can be chances that the 3 min read How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu 3 min read Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read Nested Decorators in Python Everything in Python is an object. Even function is a type of object in Python. Decorators are a special type of function which return a wrapper function. They are considered very powerful in Python and are used to modify the behaviour of a function temporarily without changing its actual value. Nes 2 min read Dispatch Decorator in Python Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. Example: Python3 # defining a deco 2 min read Like