Skip to content

Instantly share code, notes, and snippets.

@matthewdeanmartin
Created February 20, 2020 21:09
Show Gist options
  • Save matthewdeanmartin/44b310b88eeb69c0ab0b312e48753126 to your computer and use it in GitHub Desktop.
Save matthewdeanmartin/44b310b88eeb69c0ab0b312e48753126 to your computer and use it in GitHub Desktop.
from typing import Callable
def try_catch(to_try:Callable, to_catch:Callable, exception_type:Exception, to_finally:Callable):
try:
to_try()
except exception_type as exception:
to_catch(exception)
finally:
to_finally()
def example():
message_via_closure = "oops"
def try_it():
raise TypeError(message_via_closure)
def catch_it(exception:Exception):
print(message_via_closure + " 2")
print(str(exception) + " is the exception message")
def finally_it():
print(message_via_closure + " 3")
print("closing connection")
try_catch(try_it, catch_it, TypeError, finally_it)
example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment