Skip to content

Instantly share code, notes, and snippets.

@dexpota
Last active August 2, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexpota/160d6edacfcccac5a9aa to your computer and use it in GitHub Desktop.
Save dexpota/160d6edacfcccac5a9aa to your computer and use it in GitHub Desktop.
Exception handling using a decorator
from exception_handling import exceptions
from random import randint
def dummy_handler(e):
print("Exception handling ...")
return True
def else_handler():
print("Else handling ...")
def finally_handler():
print("Finally handling ...")
@exceptions((ValueError, dummy_handler),
(IOError, dummy_handler),
(AssertionError, dummy_handler),
else_function=else_handler,
finally_function=finally_handler)
def foo():
rand = randint(0, 4)
if rand == 0:
raise AssertionError()
elif rand == 1:
raise ValueError()
elif rand == 2:
raise IOError()
elif rand == 3:
raise AttributeError()
elif rand == 4:
return rand
print(foo())
import sys
from functools import wraps
def exceptions(*exc_list,
else_function=None,
finally_function=None):
def _exceptions(function):
def _decorator(*args, **kwargs):
try:
result = function(*args, **kwargs)
except:
exc_object = sys.exc_info()[1]
exc_handlers = filter(lambda e:
isinstance(exc_object, e[0]), exc_list)
exc_handler = next(exc_handlers, None)
if exc_handler is None:
raise
if not exc_handler[1](exc_object):
raise
else:
if else_function is not None:
else_function()
return result
finally:
if finally_function is not None:
finally_function()
return wraps(function)(_decorator)
return _exceptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment