[Python-Dev] Handle errors in cleanup code
gzlist at googlemail.com
Mon Jun 12 03:31:02 EDT 2017
On 12/06/2017, Serhiy Storchaka <storchaka at gmail.com> wrote:
> But if an error is raised when execute undo_something(), it replaces the
> original exception which become chaining as the __context__ attribute.
> The problem is that this can change the type of the exception. If
> do_something_other() raises SystemExit and undo_something() raises
> KeyError, the final exception has type KeyError.
For Python 2.7, I've used the following idiom, which always masks
errors from the undo:
Unfortunately, that breaks down on Python 3, as the new exception
propogates with the original as context..
> Does it mean that we should rewrite every chunk of code similar to the
> above? And if such cumbersome code is necessary and become common, maybe
> it needs syntax support in the language? Or changing the semantic of
> exceptions raised in error handlers and finally blocks?
What I want is a way to chain exceptions in the other direction,
raising the original error, but attaching a related one. Unfortunately
neither __cause__ or __context__ really help.
More information about the Python-Dev