📄 language.exceptions.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Exceptions</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.rules.html">Name resolution rules</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.references.html">References Explained</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Exceptions</h1> <p class="para"> PHP 5 has an exception model similar to that of other programming languages. An exception can be <i>throw</i>n, and caught ("<i>catch</i>ed") within PHP. Code may be surrounded in a <i>try</i> block, to facilitate the catching of potential exceptions. Each <i>try</i> must have at least one corresponding <i>catch</i> block. Multiple <i>catch</i> blocks can be used to catch different classes of exeptions. Normal execution (when no exception is thrown within the <i>try</i> block, or when a <i>catch</i> matching the thrown exception's class is not present) will continue after that last catch block defined in sequence. Exceptions can be <i>throw</i>n (or re-thrown) within a <i>catch</i> block. </p> <p class="para"> When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching <i>catch</i> block. If an exception is not caught, a PHP Fatal Error will be issued with an "<i>Uncaught Exception ...</i>" message, unless a handler has been defined with <a href="function.set-exception-handler.html" class="function">set_exception_handler()</a>. </p> <div class="example"> <p><b>Example #1 Throwing an Exception</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">$x</span><span style="color: #007700">) {<br /> throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Division by zero.'</span><span style="color: #007700">);<br /> }<br /> else return </span><span style="color: #0000BB">1</span><span style="color: #007700">/</span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />}<br /><br />try {<br /> echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">inverse</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Caught exception: '</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Continue execution<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello World'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>0.2Caught exception: Division by zero.Hello World</pre></div> </pre></div> </div> <div class="example"> <p><b>Example #2 Nested Exception</b></p> <div class="example-contents">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -