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

📄 93.html

📁 Python Ebook Python&XML
💻 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 Developer's Handbook -&gt; Exception Handling</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="91.html" class="navtitle">4. Exception Handling</a> &gt; <span class="nonavtitle">Exception Handling</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="92.html" title="4. Exception Handling"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=93" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="93.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="94.html" title="Standard Exceptions (Getting Help from Other Modules)"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A31%3A40+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147037129069031188131198</font><a href="read7.asp?bookname=0672319942&snode=93&now=5%2F31%2F2002+4%3A31%3A40+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Exception Handling</h3>
				<p>Exceptions are mostly used for error handling and event notification. They work by breaking the regular flow of a program and jumping to a special set of statements that handle the exception case. Python has many standard exceptions, which are exceptions already built into the language. Python also supports user-defined exceptions, which are exceptions created by users. The provided exceptions are almost no different from user-defined exceptions梩he only difference is that they are defined in one of the files in the standard library (<tT CLAss="monofont">exceptions.py</tt>).</P>

				<P>Any unexpected program behavior drives the interpreter to raise an exception. Many scenarios can help an exception to be raised, such as dividing a number by zero or reading from a nonexistent file. Note that the programmer can also manually <A Name="idx1073743079"></a><A NAMe="idx1073743080"></a>raise exceptions with the <a name="idx1073743081"></a><a name="idx1073743082"></a><tt clasS="monofont">raise</tt> statement.</P>

				<p>The default behavior of Python, when it encounters unhandled exceptions, is to terminate the program and to display a traceback message that describes the error condition. My goal in this chapter is to show you how to handle those exceptions.</p>

				<p>If you don't handle exceptions in your program, Python's interpreter returns a traceback message that shows the error message, the exception type, the function that contains the error, and the line of code that has caused the error. Hence, a complete history of what has caused the error is provided.<A name="idx1073743083"></A><a naME="idx1073743084"></A></P>

				<p>So that you can start learning how Python raises and handles exceptions, I will define the following example:</p>

				<prE>
					
&gt;&gt;&gt; a = { "a":1,"b":2}
&gt;&gt;&gt; def returnelement(element):

⌨️ 快捷键说明

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