[issue15806] Add context manager for the "try: ... except: pass" pattern

Ezio Melotti report at bugs.python.org
Wed Aug 29 12:07:00 CEST 2012


Ezio Melotti added the comment:

> As with many context managers, a key benefit here is 
> in the priming effect for readers.

The "focus" is mostly on what it's being executed rather than what it's being ignored though.

"Do this operation and ignore these exceptions if they occur"
vs.
"Ignore these exceptions if they occur while doing this operation."

> I'm not worried that it makes things less explicit - it's pretty
> obvious what a context manager called "ignored" that accepts an
> arbitrary number of exceptions is going to do.

It's still understandable, but while I'm familiar with the semantics of try/except, I wouldn't be sure if e.g. this just ignored those specific exceptions or even their subclasses without checking the doc/code.

> One other thing it does is interact well with ExitStack - you can
> stick this in the stack of exit callbacks to suppress exceptions that
> you don't want to propagate.

This seems a good use case.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15806>
_______________________________________


More information about the Python-bugs-list mailing list