PHP中的Try/Catch區塊無法捕捉異常的情況
<p>我正在嘗試運行這個例子#1,來自這個頁面:http://php.net/manual/en/language.exceptions.php</p>
<pre class="brush:php;toolbar:false;"><?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "n";
echo inverse(0) . "n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "n";
}
// Continue execution
echo "Hello Worldn";
?></pre>
<p>然而,我得到的輸出不是預期的:</p>
<pre class="brush:php;toolbar:false;">0.2
Fatal error: Uncaught exception 'Exception' with message 'Division by zero.'
in xxx:
7 Stack trace: #0 xxx(14): inverse(0) #1 {main} thrown in xxx on line 7</pre>
<p>我使用的開發環境是<code>UniServer 3.5</code>和<code>PHP 5.2.3</code></p>