Package: exception-handling-decorator
ExceptionHandler: Decorator for Exception HandlingExceptionHandler is an open-source Python library for simplyfying exception handling. It makes Python handling exception code cleaner and more readable.InstallationTo installExceptionHandler, usepipwith the following command:pip install exception-handling-decoratorReleasesLatest releases of ExceptionHandler can be found at the PyPI repository:ExceptionHandler on PyPISource CodeThe source code is stored at the GitHub repository:ExceptionHandler on GitHubGetting startedImport ExceptionHandler:fromexception_handlerimportExceptionHandlerUse @ExceptionHandler decorator with the following parameters:exception=Exception- Exception(s) to be cР°ught. It can be of typeExceptionor its descendant ortuple[Exception].handling_func=None- Function(s) to handle exception(s). It can be of typeCallableor its descendant ortuple[Callable]. If handling_func isNone, the exception will not be handled. If there more than one handling function (tuple[Callable]), every function will handle the correspondingException.reraise=True- Indicates if the excaption(s) should be reraised. It can be of typeboolor its descendant ortuple[bool]. If there more than onereraise(tuple[bool]), every tuple member will raise the correspondingException.Example 1The following code handles a ZeroDivisionError with classic try/except catching, prints and re-raise the exception:defprint_exception(ex):print(ex)defdivide_by_zero():try:return1/0exceptZeroDivisionErrorase:print_exception(e)raisedivide_by_zero()We can simplify divide_by_zero() function by using@ExceptionHandlerdecorator:@ExceptionHandler(exception=ZeroDivisionError,handling_func=print_exception,reraise=True)defdivide_by_zero():return1/0The final code:fromexception_handlerimportExceptionHandlerdefprint_exception(ex):print(ex)@ExceptionHandler(exception=ZeroDivisionError,handling_func=print_exception,reraise=True)defdivide_by_zero():return1/0divide_by_zero()Example 2The following code raises randomly one of two custom exceptions:importrandomimportloggingclassSomeException(Exception):def__str__(self)->str:return"SomeException message"classSomeOtherException(Exception):def__str__(self)->str:return"SomeOtherException message"defget_rand_bool():"""Returns random boolean value"""rand_int=random.getrandbits(1)returnbool(rand_int)deflog_exception(ex):logging.error(ex)defprint_exception(ex):print(ex)defdo_something():ifget_rand_bool():try:raiseSomeExceptionexceptSomeExceptionase:print_exception(e)raisetry:raiseSomeOtherExceptionexceptSomeOtherExceptionase:log_exception(e)do_something()We can simplify do_something() function by using@ExceptionHandlerdecorator:@ExceptionHandler(exception=(SomeException,SomeOtherException),handling_func=(print_exception,log_exception),reraise=(True,False))defdo_something():ifget_rand_bool():raiseSomeExceptionraiseSomeOtherExceptionThe final code:importrandomfromexception_handlerimportExceptionHandlerimportloggingclassSomeException(Exception):def__str__(self)->str:return"SomeException message"classSomeOtherException(Exception):def__str__(self)->str:return"SomeOtherException message"defget_rand_bool():"""Returns random boolean value"""rand_int=random.getrandbits(1)returnbool(rand_int)deflog_exception(ex):logging.error(ex)defprint_exception(ex):print(ex)@ExceptionHandler(exception=(SomeException,SomeOtherException),handling_func=(print_exception,log_exception),reraise=(True,False))defdo_something():ifget_rand_bool():raiseSomeExceptionraiseSomeOtherExceptiondo_something()