⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13s04.html

📁 python不错的入门书籍
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 异常 / try..finally </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第13章 异常</th><tr><th width="20%" align="left"><a href="ch13s03.html">上一页</a></th><th width="60%" align="center"><span class="header2">try..finally</span></th><th align="right"><a href="ch13s05.html">下一页</a></th></tr></table><hr noshade><h1>try..finally</h1><p>假如你在读一个文件的时候,希望在无论异常发生与否的情况下都关闭文件,该怎么做呢?这可以使用<code>finally</code>块来完成。注意,在一个<code>try</code>块下,你可以同时使用<code>except</code>从句和<code>finally</code>块。如果你要同时使用它们的话,需要把一个嵌入另外一个。</p><h2><a name="using">使用finally</a></h2><p class="exampletitle"><a name="e133">例13.3 使用finally</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: finally.py</code><br><br><code class="key">import </code><code class="func">time</code><br><br><code class="key">try</code><code>:<br>&nbsp;&nbsp;&nbsp;&nbsp;f = </code><code class="func">file</code><code>(</code><code class="cite">'poem.txt'</code><code>)</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;while </code><code class="func">True</code><code>: </code><code class="comment"># our usual file-reading idiom</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = f.readline()</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if </code><code class="func">len</code><code>(line) == </code><code class="cite">0</code><code>:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break</code><br><code class="func">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time</code><code>.sleep(</code><code class="cite">2</code><code>)</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </code><code>line,</code><br><code class="key">finally</code><code>:<br>&nbsp;&nbsp;&nbsp;&nbsp;f.close()</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;print </code><code class="cite">'Cleaning up...closed the file'</code></p><p>(源文件:<a href="code/finally.py">code/finally.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python finally.py<br>Programming is fun<br>When the work is done<br>Cleaning up...closed the file<br>Traceback (most recent call last):<br>&nbsp;&nbsp;File "finally.py", line 12, in ?<br>&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)<br>KeyboardInterrupt</code></p><h2>它如何工作</h2><p>我们进行通常的读文件工作,但是我有意在每打印一行之前用<code>time.sleep</code>方法暂停2秒钟。这样做的原因是让程序运行得慢一些(Python由于其本质通常运行得很快)。在程序运行的时候,按<strong>Ctrl-c</strong>中断/取消程序。</p><p>我们可以观察到<code>KeyboardInterrupt</code>异常被触发,程序退出。但是在程序退出之前,finally从句仍然被执行,把文件关闭</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch13s03.html">上一页</a></th><th width="60%" align="center"><a href="ch13.html">上一级</a></th><th width="20%" align="right"><a href="ch13s05.html">下一页</a></th></tr><tr><th width="20%" align="left">引发异常</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">概括</th></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -