发布于 2015-08-30 07:50:08 | 130 次阅读 | 评论: 0 | 来源: 网络整理

问题

You want to write code that catches all exceptions.


解决方案

To catch all exceptions, write an exception handler for Exception, as shown here:

try:
...
except Exception as e:
... log(‘Reason:’, e) # Important!

This will catch all exceptions save SystemExit, KeyboardInterrupt, and GeneratorEx it. If you also want to catch those exceptions, change Exception to BaseException.


讨论

Catching all exceptions is sometimes used as a crutch by programmers who can’t re‐ member all of the possible exceptions that might occur in complicated operations. As such, it is also a very good way to write undebuggable code if you are not careful. Because of this, if you choose to catch all exceptions, it is absolutely critical to log or report the actual reason for the exception somewhere (e.g., log file, error message print‐ ed to screen, etc.). If you don’t do this, your head will likely explode at some point. Consider this example:

def parse_int(s):
try:
n = int(v)
except Exception:
print(“Couldn’t parse”)

If you try this function, it behaves like this:

>>> parse_int('n/a')
Couldn't parse
>>> parse_int('42')
Couldn't parse
>>>

At this point, you might be left scratching your head as to why it doesn’t work. Now suppose the function had been written like this:

def parse_int(s):
try:
n = int(v)
except Exception as e:
print(“Couldn’t parse”) print(‘Reason:’, e)

In this case, you get the following output, which indicates that a programming mistake has been made:

>>> parse_int('42')
Couldn't parse
Reason: global name 'v' is not defined
>>>

All things being equal, it’s probably better to be as precise as possible in your exception handling. However, if you must catch all exceptions, just make sure you give good di‐ agnostic information or propagate the exception so that cause doesn’t get lost.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务