python generic exception handling and return arg on exception
- by rikAtee
I am trying to create generic exception handler - for where I can set an arg to return in case of exception, inspired from this answer.
import contextlib
@contextlib.contextmanager
def handler(default):
try:
yield
except Exception as e:
yield default
def main():
with handler(0):
return 1 / 0
with handler(0):
return 100 / 0
with handler(0):
return 'helllo + 'cheese'
But this results in
RuntimeError: generator didn't stop after throw()