Sven Marnach dixit (2011-10-20, 16:11):
Why introduce an error handler at all? If you want to handle exceptions inside the with statement, an explicit try-except block would be both more convenient (you don't need an extra function definition) and more flexible (you can use - say - 'except ValueError:' to just catch some exceptions). To give an example, I'd really prefer
with CleanupManager() as cleanup: try: # do stuff except ValueError: # handle exception
You are right, that error handler is redundant.
Except that, my code mimics the nested structure of try-finally clauses from the primary OP's example.