page607.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 78 行
HTML
78 行
<HTML><HEAD><TITLE>Exceptions</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html8121" HREF="page608.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8119" HREF="page585.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8115" HREF="page606.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8123" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0017700000000000000000">Exceptions</A></H1><A NAME="secpythonexceptions"> </A><P>Sometimes unexpected situations arise during the execution of a program.Careful programmers write code that detects errors anddeals with them appropriately.However, a simple algorithm can become unintelligiblewhen error-checking is added because the error-checking codecan obscure the normal operation of the algorithm.<P><em>Exceptions</em><A NAME=57449> </A> provide a clean way to detectand handle unexpected situations.When a program detects an error,it <em>raises</em><A NAME=57451> </A> an exception.When an exception is raised,control is transfered to the appropriate<em>exception handler</em><A NAME=57453> </A>.By defining a method that <em>catches</em> the exception,the programmer can write the code to handle the error.<P>In Python, an exception is an object.All exceptions in Python are ultimately derived from the built-inbase class called <tt>Exception</tt>.For example,consider the class <tt>A</tt> defined in Program <A HREF="page607.html#progexamplew"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Since the <tt>A</tt> class extends the <tt>Exception</tt> class,<tt>A</tt> is an exception that can be <em>raised</em>.<P><P><A NAME="57465"> </A><A NAME="progexamplew"> </A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program57462" SRC="img2497.gif" ><BR><STRONG>Program:</STRONG> Using exceptions in Python.<BR><P><P>A method raises an exception by using the <tt>raise</tt> statement:The <tt>raise</tt> statement is similar to a <tt>return</tt> statement.A <tt>return</tt> statement represents the normal terminationof a method and the object returned matches the return value of the method.A <tt>raise</tt> statement represents the abnormal termination of a methodand the object raised represents the type of error encountered.The <tt>f</tt> method in Program <A HREF="page607.html#progexamplew"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> raisess an <tt>A</tt> exception.<P>Exception handlers are defined using a <tt>try</tt> block:The body of the <tt>try</tt> block is executedeither until an exception is raised or until it terminates normally.One or more exception handlers follow a <tt>try</tt> block.Each exception handler consists of an <tt>except</tt> clausewhich specifies the exceptions to be caught,and a block of code,which is executed when the exception occurs.When the body of the <tt>try</tt> block raises an exceptionfor which an exception is defined,control is transfered to the body of the exception handler.<P>In this example, the exception raised by the <tt>f</tt> methodis caught by the <tt>g</tt> method.In general when an exception is raised,the chain of methods called is searched in reverse(from caller to callee)to find the closest matching <tt>except</tt> clause.When a program raises an exception that is not caught,the program terminates.<P><HR><A NAME="tex2html8121" HREF="page608.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8119" HREF="page585.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8115" HREF="page606.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html8123" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?