Add `finally as e`

The reason that PEP 8 and linters advise against using except: is that it catches all exceptions, and any code can raise exceptions that you should treat as critical (in the sense that the program should not ignore them). The usual examples are KeyboardInterrupt and MemoryError, but there are other cases. Simply logging that fact that someone requested to exit the program with a ctrl-C and moving on doesn’t make sense.

Strictly speaking, it’s the exception that is in-flight when the finally block executes. This is any exception that was not caught, caught and reraised, or a new exception raised from an except clause. If finally as e is confusing, maybe finally e (as I originally proposed) works better.

This is proposed as an alternative to

except:
   ...
   raise

If you look for this pattern (say in the stdlib) you will find it used quite a bit.

3 Likes