擅长:python、mysql、java
<p>在运行python程序时按Ctrl</kbd>+<kbd>c</kbd>将导致python引发<a href="http://docs.python.org/library/exceptions.html#exceptions.KeyboardInterrupt" rel="noreferrer">^{<cd1>}</a>异常。一个发出大量HTTP请求的程序可能会有大量异常处理代码。如果<code>try</code>-<code>except</code>块的<code>except</code>部分没有指定它应该捕获哪些异常,它将捕获所有异常,包括您刚才导致的<code>KeyboardInterrupt</code>。正确编码的python程序将使用<a href="http://docs.python.org/library/exceptions.html#exception-hierarchy" rel="noreferrer">python exception hierarchy</a>,并且只捕获从<code>Exception</code>派生的异常。</p>
<pre><code>#This is the wrong way to do things
try:
#Some stuff might raise an IO exception
except:
#Code that ignores errors
#This is the right way to do things
try:
#Some stuff might raise an IO exception
except Exception:
#This won't catch KeyboardInterrupt
</code></pre>
<p>如果无法更改代码(或需要终止程序以使更改生效),则可以尝试快速按<kbd>Ctrl</kbd>+<kbd>c</kbd>。第一个<code>KeyboardInterrupt</code>异常将把您的程序从<code>try</code>块中敲出,希望当程序在<code>try</code>块之外时,会引发后面的<code>KeyboardInterrupt</code>异常之一。</p>