擅长:python、mysql、java
<p>您应该能够使用<code>try</code>/<code>finally</code>块,而不使用异常处理程序。它不会捕获异常或禁止回溯,只需确保无论是否存在异常,清理代码都是运行的。这就是<code>finally</code>的全部要点。在</p>
<p>下面是一个例子:</p>
<pre><code>Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... print 'begin try'
... assert False
... print 'end try'
... finally:
... print 'finally'
...
begin try
finally
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AssertionError
</code></pre>