pycodestyle icon indicating copy to clipboard operation
pycodestyle copied to clipboard

E722: (bare excepts) too strict (reraise)

Open snoack opened this issue 7 years ago • 17 comments

How I read PEP-8, it doesn't outlaw bare excepts, but merely recommends to catch more specific exceptions when possible. But what if there is no particular exception you want to catch, but when you just want to do some cleanup before propagating any exception?

try:
    self.connection.send(...)
except:
    # We only close the connection on failure, otherwise we keep reusing it.
    self.connection.close()
    raise

Using try...finally isn't an option here, since we want to reuse the resource on success, and only clean up on failure. I could just explicitly catch BaseException instead, but there is no indication in PEP-8 that this is preferable (otherwise why would bare except be supported in the first place).

So how about suppressing E722 if there is a raise statement in the except block?

snoack avatar Nov 04 '17 04:11 snoack