On 2021-04-08 11:25, Chris Angelico wrote:
On Fri, Apr 9, 2021 at 2:24 AM Paul Bryan email@example.com wrote:
Q. Safe to assume this would catch exceptions from both the call to `open` as well as the call to `open.__enter__`?
No, not safe to assume. It's equally reasonable to define it as guarding only the body of the statement, or as guarding the header as well. The semantics have to be locked in one way or the other, and half the time that's going to be incorrect.
Hmm, I don't see the problem.
I'd assume/want it to catch everything not already caught by the context manager. The scope would be everything inside the "try-with..." and not be mandatory of course. That's why the original example "try ..." comes first and is on the outside.
If you wanted it to guard only *inside* the with statement, then you'd not be putting the try on the outside in the first place. You'd put it inside.
I've used try-with many, many times, and can't currently remember any instances of with-try.