Is it possible to define an own with
statement which automatically includes try...except error handling?
For example, it would be neat to have a shorthand for this:
with Do_Something():
try:
...
except Exception as e:
print(str(e))
...that looks like this:
with Try_Something():
...
How can we include the try...except behaviour into the following MWE class?
class Do_Something():
def __init__(self):
pass
def __enter__(self):
print('Starting...')
# invoke "try" somewhere here?
return(self)
def __exit__(self, except_type, except_value, tb):
# invoke "except" somewhere here?