class with_test():
def __enter__(self):
print('With Enter')
return 'Enter Reference'
def __exit__(self, *args):
print('With Exit:', args)
if args[0]:
raise
print('* Try and catch statement outside with *')
try:
with with_test() as f:
print('Inside with code:', f)
raise Exception('Testing Exception')
print('After with code:', f)
except Exception as e:
print('Exception handling with exception:', e, f)
print('\n* Try and catch statement inside with *')
with with_test() as f:
try:
print('Inside with code:', f)
raise Exception('Testing Exception')
except Exception as e:
print('Exception handling:', e, f)
"""
* Try and catch statement outside with *
With Enter
Inside with code: Enter Reference
With Exit: (<class 'Exception'>, Exception('Testing Exception',), <traceback object at 0x000002CE6B64F908>)
Exception handling with exception: Testing Exception Enter Reference
* Try and catch statement inside with *
With Enter
Inside with code: Enter Reference
Exception handling: Testing Exception Enter Reference
With Exit: (None, None, None)
"""
2019-02-14