[Python-Dev] Handle errors in cleanup code
storchaka at gmail.com
Mon Jun 12 03:10:28 EDT 2017
There is an idiomatic Python code:
If an error is raised when execute do_something_other(), then we should
first restore the state that was before calling do_something(), and then
reraise the exception. It is important that the bare "except" (or
"except BaseException") is used, because undo_something() should be
executed on any exception. And this is one of few cases where using the
bare "except" is correct.
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.
Yury in the comment for PR 2108  suggested more complicated code:
except BaseException as ex:
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?
More information about the Python-Dev