Does `try ... except Exception as e` use all possible exceptions?

In Python 2, are all exceptions that raise d may be necessary to inherit from Exception?

That is, enough to catch any possible exception:

 try: code() except Exception as e: pass 

or do I need something even more general, like

 try: code() except: pass 
+5
source share
1 answer

In the first option, you will catch "all built-in exceptions that are system-independent" ( https://docs.python.org/2/library/exceptions.html ) and should catch user-defined exceptions ("all user-defined exceptions should also be received from this class ").

For example, the first option will not capture the Control-C (KeyboardInterrupt) pressed by the user, but the second will.

+6
source

Source: https://habr.com/ru/post/1207777/


All Articles