Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Last active August 29, 2015 14:24
Show Gist options
  • Save hellpanderrr/2bdcb13baea433ec8376 to your computer and use it in GitHub Desktop.
Save hellpanderrr/2bdcb13baea433ec8376 to your computer and use it in GitHub Desktop.
Python handling exception in a context manager
from contextlib import contextmanager

@contextmanager
def catch(*exceptions, **kwargs):
    try:
        yield kwargs.get("default", None)
    except exceptions or Exception:
        print kwargs
        
#Example
with catch(**{'Variable not found' : 'Variable_name' }) as _:
    some_variable = function_to_get_something(bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment