Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created October 26, 2018 15:23
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 hughdbrown/2260087e58577935e1b441d2064fbb6b to your computer and use it in GitHub Desktop.
Save hughdbrown/2260087e58577935e1b441d2064fbb6b to your computer and use it in GitHub Desktop.
Decorator that can track usage through exceptions
def a(throw_please=False):
_ret = None
_exc = None
try:
print("try")
if throw_please:
b = []
b[1] = 3
_ret = 4
except Exception as exc:
print("except")
_exc = exc
raise
else:
print("else")
return _ret
finally:
print("finally fn_name: |{2}| ret: |{0}| exc: |{1}|".format(_ret, _exc, a.func_name))
print("a() = {0}".format(a(False)))
print("a() = {0}".format(a(True)))
"""
>>> print("a() = {0}".format(a(False)))
try
else
finally fn_name: |a| ret: |4| exc: |None|
a() = 4
>>> print("a() = {0}".format(a(True)))
try
except
finally fn_name: |a| ret: |None| exc: |list assignment index out of range|
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in a
IndexError: list assignment index out of range
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment