擅长:python、mysql、java
<p>如果内部try中的filter语句引发异常,首先将对照内部的“except”语句集对其进行检查,然后如果没有一个语句捕获它,则将对照外部的“except”语句集对其进行检查。</p>
<p>你可以通过做这样简单的事情来说服自己(这只会打印“捕捉到值错误”):</p>
<pre><code>try:
try:
raise ValueError('1')
except TypeError:
print 'Caught the type error'
except ValueError:
print 'Caught the value error!'
</code></pre>
<p>另一个例子是,这个应该只打印“捕获内部值错误”:</p>
<pre><code>try:
try:
raise ValueError('1')
except TypeError:
pass
except ValueError:
print 'Caught the inner ValueError!'
except ValueError:
print 'Caught the outer value error!'
</code></pre>