In Python 2, are all exceptions that can be raise
d required to inherit from Exception?
That is, is the following sufficient 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
With the first variant you'll catch "all built-in, non-system-exiting exceptions" ( https://docs.python.org/2/library/exceptions.html ), and should catch user defined exceptions ("all user-defined exceptions should also be derived from this class").
For example, the first variant will not catch user-pressed Control-C (KeyboardInterrupt), but the second will.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.