A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2017-June/148318.html below:

[Python-Dev] Handle errors in cleanup code

[Python-Dev] Handle errors in cleanup codeMartin (gzlist) 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:

      do_something()
      try:
          do_something_other()
      except:
          try:
              undo_something()
          finally:
              raise

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.

Martin
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4