On Jul 30, 2019, at 11:38 AM, Guido van Rossum <guido@python.org> wrote:
...
with connect() as stream: # connect() or __enter__() can fail. for data in stream: # __next__() can fail write(data) # write() can fail
This very much looks like toy networking code to me -- real networking code is always written in a completely different way, using different abstractions that raise different exceptions, and I don't think it would be written in this style even if `with` and `for` had optional `except` clauses. (Alternatively, you can write little wrappers that turn OSError into different exceptions so you can use a single try/except/except/except statement to handle them all.)
This is what I do: wrappers with custom exceptions. I think it gives the most readable code. But I definitely do not need this pattern very often. Eric