[Python-Dev] Why not using "except: (...) raise" to cleanup on error?
rosuav at gmail.com
Mon Jun 4 12:42:09 EDT 2018
On Tue, Jun 5, 2018 at 2:11 AM, Victor Stinner <vstinner at redhat.com> wrote:
> I just read a recent bugfix in asyncio:
> + try:
> + await waiter
> + except Exception:
> + transport.close()
> + raise
> Why only catching "except Exception:"? Why not also catching
> KeyboardInterrupt or MemoryError? Is it a special rule for asyncio, or
> a general policy in Python stdlib?
> For me, it's fine to catch any exception using "except:" if the block
> contains "raise", typical pattern to cleanup a resource in case of
> error. Otherwise, there is a risk of leaking open file or not flushing
> data on disk, for example.
Pardon the dumb question, but why is try/finally unsuitable?
More information about the Python-Dev