Context Manager that handles exceptions
问题 I am trying to wrap my head around how to write a context manager that deals with writing some logs while handling any exceptions. The problem I am trying to solve is to make code like this: try: # code that can raise exception here except Exception as e: print('failed', e) print('all good') This is a repeated pattern I have in the code and I think it's best handled with a context manager like: with my_ctx_manager(success_msg='all good', failed_msg='failed): # code that can raise exception