correct way to catch exception with Python 'with' statement
Peter Otten
__peter__ at web.de
Tue Nov 29 04:06:33 EST 2016
Marko Rauhamaa wrote:
> However, I think the real answer is that you shouldn't mix the "with"
> construct with exception handling. Instead you should write:
>
> try:
> f = open("xyz")
> except FileNotFoundError:
> ...[B]...
> try:
> ...[A]...
> finally:
> f.close()
What's the problem with spelling the above
try:
f = open(...)
except FileNotFoundError:
...
with f:
...
?
More information about the Python-list
mailing list