擅长:python、mysql、java
<p>您可以引发对象,捕获异常,然后使用<code>is</code>关键字检查引发的异常是否是对象或对象的实例。如果引发了其他任何内容,则为<code>TypeError</code>,表示该对象不可提升。在</p>
<p>此外,要处理任何可提升的对象,我们可以使用<a href="https://docs.python.org/2/library/sys.html#sys.exc_info" rel="nofollow noreferrer">^{<cd3>}</a>。这还将捕捉异常,例如<code>KeyboardInterrupt</code>,但是如果与参数的比较没有结果,我们可以重新调用它们。在</p>
<pre><code>import sys
def is_raisable(obj):
try:
raise obj
except:
exc_type, exc = sys.exc_info()[:2]
if exc is obj or exc_type is obj:
return True
elif exc_type is TypeError:
return False
else:
# We reraise exceptions such as KeyboardInterrupt that originated from outside
raise
is_raisable(ValueError) # True
is_raisable(KeyboardInterrupt) # True
is_raisable(1) # False
</code></pre>