python语言引用以section 7.4表示:
For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception.
那么,为什么except object:
不能捕获所有内容?object
是所有异常类的基类,因此except object:
应该能够捕获每个异常。在
例如,这应该捕捉AssertionError
print isinstance(AssertionError(), object) # prints True
try:
raise AssertionError()
except object:
# This block should execute but it never does.
print 'Caught exception'
我相信答案可以在source code for python 2.7中找到:
因此,如果
w
(我假设except
语句中的表达式)不是元组或异常类,并且设置了Py_Py3kWarningFlag
,那么尝试在except块中使用无效的异常类型将显示警告。在该标志是通过在执行文件时添加
^{pr2}$-3
标志来设置的:相关问题 更多 >
编程相关推荐