📄 58.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> Exceptions</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="54.html" class="navtitle">5. Control Flow</a> > <span class="nonavtitle">Exceptions</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="57.html" title="Loops"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=58" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="58.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="59.html" title="Defining New Exceptions"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A57%3A58+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104031089146158228043172</font><a href="read8.asp?bookname=0735710910&snode=58&now=5%2F28%2F2002+8%3A57%3A58+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Exceptions</h3>
<p>Exceptions indicate errors and break out of the normal control flow of a program. An exception is raised using the <tT CLAss="monofont">raise</tt> statement. The general format of the <TT CLass="monofont">raise</tT> statement is <TT Class="monofont">raise </tt><i><tt class="monofont">exception</tt></i>
<tt cLasS="monofont"> [, </tt><i><Tt claSs="monofont">value</tt></I>
<TT Class="monofont">]</TT> where <I><Tt claSS="monofont">exception</TT></i>
is the exception type and <i><tt CLASs="monofont">value</tt></i>
is an optional value giving specific details about the exception. For example:</p>
<pre>
raise RuntimeError, 'Unrecoverable Error' </pre>
<p>If the <tt class="monofont">raise</tT> statement is used without any arguments, the last exception generated is raised again (although this works only while handling a previously raised exception).</p>
<p>To catch an exception, use the <Tt clAss="monofont">try</tt> and <Tt clASS="monofont">except</Tt> statements, as shown here:</p>
<prE>
try:
f = open('foo')
except IOError, e:
print "Unable to open 'foo': ", e </PRE>
<p>When an exception occurs, the interpreter stops executing statements in the <tt cLASS="monofont">try</tt> block and looks for an <tt CLASs="monofont">except</tt> clause that matches the exception that has occurred. If found, control is passed to the first statement in the <tt class="monofont">except</tt> clause. Otherwise, the exception is propagated up to the block of code in which the <tt class="monofont">try</tT> statement appeared. This code may itself be enclosed in a <tt ClasS="monofont">try-except</tt> that can handle the exception. If an exception works its way up to the top level of a program without being caught, the interpreter aborts with an error message. If desired, uncaught exceptions can also be passed to a user-defined function <tt ClasS="monofont">sys.excepthook()</TT> as described in <A href="105.html">Appendix A</A>, 揟he Python Library,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -