Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 11966

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>异常处理</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="语言参考"
HREF="langref.html"><LINK
REL="PREVIOUS"
TITLE="Type Hinting"
HREF="language.oop5.typehinting.html"><LINK
REL="NEXT"
TITLE="引用的解释"
HREF="language.references.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="chapter"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.oop5.typehinting.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.references.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="language.exceptions"
>章 20. 异常处理</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>目录</B
></DT
><DT
><A
HREF="language.exceptions.html#language.exceptions.extending"
>扩展 PHP 内置的异常处理类</A
></DT
></DL
></DIV
><P
>&#13;   PHP 5 添加了类似于其它语言的异常处理模块。在 PHP 代码中所产生的异常可被
   <TT
CLASS="literal"
>throw</TT
> 语句抛出并被 <TT
CLASS="literal"
>catch</TT
>
   语句捕获。需要进行异常处理的代码都必须放入 <TT
CLASS="literal"
>try</TT
>
   代码块内,以便捕获可能存在的异常。每一个 <TT
CLASS="literal"
>try</TT
>
   至少要有一个与之对应的 <TT
CLASS="literal"
>catch</TT
>。使用多个
   <TT
CLASS="literal"
>catch</TT
> 可以捕获不同的类所产生的异常。当
   <TT
CLASS="literal"
>try</TT
> 代码块不再抛出异常或者找不到 <TT
CLASS="literal"
>catch</TT
>
   能匹配所抛出的异常时,PHP 代码就会在跳转到最后一个 catch
   的后面继续执行。当然,PHP 允许在 <TT
CLASS="literal"
>catch</TT
>
   代码块内再次抛出(<TT
CLASS="literal"
>throw</TT
>)异常。
  </P
><P
>&#13;   当一个异常被抛出时,其后(译者注:指抛出异常时所在的代码块)的代码将不会继续执行,而 PHP
   就会尝试查找第一个能与之匹配的 <TT
CLASS="literal"
>catch</TT
>。如果一个异常没有被捕获,而且又没用使用
   <A
HREF="function.set-exception-handler.html"
><B
CLASS="function"
>set_exception_handler()</B
></A
> 作相应的处理的话,那么 PHP
   将会产生一个严重的错误,并且输出
   <TT
CLASS="literal"
>Uncaught Exception ...</TT
> (未捕获异常)的提示信息。
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN6300"
></A
><P
><B
>例 20-1. 抛出一个异常</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$error </font><font color="#007700">= </font><font color="#DD0000">'Always throw this error'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">throw </font><font color="#007700">new </font><font color="#0000BB">Exception</font><font color="#007700">(</font><font color="#0000BB">$error</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 从这里开始,tra 代码块内的代码将不会被执行<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">'Never executed'</font><font color="#007700">;<br /><br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">Exception $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Caught exception: '</font><font color="#007700">,&nbsp;&nbsp;</font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">(), </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// 继续执行<br /></font><font color="#007700">echo </font><font color="#DD0000">'Hello World'</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.exceptions.extending"
>扩展 PHP 内置的异常处理类</A
></H1
><P
>&#13;    用户可以用自定义的异常处理类来扩展 PHP
    内置的异常处理类。以下的代码说明了在内置的异常处理类中,哪些属性和方法在子类中是可访问和可继承的。译者注:以下这段代码只为说明内置异常处理类的结构,它并不是一段有实际意义的可用代码。
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN6306"
></A
><P
><B
>例 20-2. 内置的异常处理类</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">Exception<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $message </font><font color="#007700">= </font><font color="#DD0000">'Unknown exception'</font><font color="#007700">;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 异常信息<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $code </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 用户自定义异常代码<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $file</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 发生异常的文件名<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $line</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 发生异常的代码行号<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$message </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">, </font><font color="#0000BB">$code </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getMessage</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 返回异常信息<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getCode</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 返回异常代码<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getFile</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 返回发生异常的文件名<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getLine</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 返回发生异常的代码行号<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getTrace</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// backtrace() 数组<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">final </font><font color="#007700">function </font><font color="#0000BB">getTraceAsString</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 已格成化成字符串的 getTrace() 信息<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/* 可重载的方法 */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">__toString</font><font color="#007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 可输出的字符串<br /></font><font color="#007700">}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    如果使用自定义的类来扩展内置异常处理类,并且要重新定义<A
HREF="language.oop5.decon.html"
>构造函数</A
>的话,建议同时调用
    <A
HREF="language.oop5.paamayim-nekudotayim.html"
>parent::__construct()</A
>
    来检查所有的变量是否已被赋值。当对象要输出字符串的时候,可以重载
    <A
HREF="language.oop5.magic.html"
>__toString()</A
> 并自定义输出的样式。
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN6313"
></A
><P
><B
>例 20-3. 扩展 PHP 内置的异常处理类</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">/**<br /> * 自定义一个异常处理类<br /> */<br /></font><font color="#007700">class </font><font color="#0000BB">MyException </font><font color="#007700">extends </font><font color="#0000BB">Exception<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 重定义构造器使 message 变为必须被指定的属性<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$message</font><font color="#007700">, </font><font color="#0000BB">$code </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 自定义的代码<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 确保所有变量都被正确赋值<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">parent</font><font color="#007700">::</font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$message</font><font color="#007700">, </font><font color="#0000BB">$code</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 自定义字符串输出的样式<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__toString</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">__CLASS__ </font><font color="#007700">. </font><font color="#DD0000">": </font><font color="#007700">[{</font><font color="#DD0000">$this</font><font color="#007700">-&gt;</font><font color="#DD0000">code</font><font color="#007700">}]</font><font color="#DD0000">: </font><font color="#007700">{</font><font color="#DD0000">$this</font><font color="#007700">-&gt;</font><font color="#DD0000">message</font><font color="#007700">}\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">customFunction</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"A Custom function for this type of exception\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /><br /></font><font color="#FF8000">/**<br /> * 创建一个用于测试异常处理机制的类<br /> */<br /></font><font color="#007700">class </font><font color="#0000BB">TestException<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;const </font><font color="#0000BB">THROW_NONE&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;const </font><font color="#0000BB">THROW_CUSTOM&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;const </font><font color="#0000BB">THROW_DEFAULT </font><font color="#007700">= </font><font color="#0000BB">2</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$avalue </font><font color="#007700">= </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">THROW_NONE</font><font color="#007700">) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (</font><font color="#0000BB">$avalue</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">THROW_CUSTOM</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 抛出自定义异常<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">throw </font><font color="#007700">new </font><font color="#0000BB">MyException</font><font color="#007700">(</font><font color="#DD0000">'1 is an invalid parameter'</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">THROW_DEFAULT</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 抛出默认的异常<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">throw </font><font color="#007700">new </font><font color="#0000BB">Exception</font><font color="#007700">(</font><font color="#DD0000">'2 isnt allowed as a parameter'</font><font color="#007700">, </font><font color="#0000BB">6</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 没有异常的情况下,创建一个对象<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var </font><font color="#007700">= </font><font color="#0000BB">$avalue</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /><br /></font><font color="#FF8000">// 例子 1<br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">TestException</font><font color="#007700">(</font><font color="#0000BB">TestException</font><font color="#007700">::</font><font color="#0000BB">THROW_CUSTOM</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">MyException $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 捕获异常<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Caught my exception\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">customFunction</font><font color="#007700">();<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">Exception $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 被忽略<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Caught Default Exception\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// 执行后续代码<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\n\n"</font><font color="#007700">;<br /><br /><br /></font><font color="#FF8000">// 例子 2<br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">TestException</font><font color="#007700">(</font><font color="#0000BB">TestException</font><font color="#007700">::</font><font color="#0000BB">THROW_DEFAULT</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">MyException $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 不能匹配异常的种类,被忽略<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Caught my exception\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">customFunction</font><font color="#007700">();<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">Exception $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 捕获异常<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Caught Default Exception\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// 执行后续代码<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\n\n"</font><font color="#007700">;<br /><br /><br /></font><font color="#FF8000">// 例子 3<br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">TestException</font><font color="#007700">(</font><font color="#0000BB">TestException</font><font color="#007700">::</font><font color="#0000BB">THROW_CUSTOM</font><font color="#007700">);<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">Exception $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 捕获异常<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Default Exception caught\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// 执行后续代码<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\n\n"</font><font color="#007700">;<br /><br /><br /></font><font color="#FF8000">// 例子 4<br /></font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">TestException</font><font color="#007700">();<br />} </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">Exception $e</font><font color="#007700">) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 没有异常,被忽略<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#DD0000">"Default Exception caught\n"</font><font color="#007700">, </font><font color="#0000BB">$e</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// 执行后续代码<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\n\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.oop5.typehinting.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.references.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Type Hinting</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="langref.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>引用的解释</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>