I use some code from the PHP manual to make an exception test, but I get some weird message.
Here it is the code:
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
else 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 World';
And here it is the output:
0.2
( ! ) Exception: Division by zero. in /var/www/OOPlearing/slash.php on line 10Call Stack#TimeMemoryFunctionLocation10.0002330188{main}( )../slash.php:020.0002330232inverse( $x = 0 )../slash.php:17Dump $_SERVER$_SERVER['HTTP_HOST'] =string 'localhost' (length=9)$_SERVER['SERVER_NAME'] =string 'localhost' (length=9)Dump $_GETVariables in local scope (#2)$x =int 0
Caught exception: Division by zero.Hello World
It’s strange that although the exception has been caught, the exception message is still on…
Some of my local settings in php.ini:
error_reporting = E_ALL & ~E_DEPRECATED
display_errors =On
display_startup_errors = Off
log_errors = Off
......
html_errors = On
My notebook:
ubuntu11.04
mysql Ver 14.14 Distrib 5.1.54
PHP 5.3.5-1
Update(2012.1.16) It’s the xdebug extension that resulted in such error outputs. The xdebug default shows stack traces on error conditions. For those who want to disable it can do below instruction:
xdebug_disable();//put it on the header of your code,like cofig file.
When you try/catch() a block of code then you wont get a fatal error of “Uncaught exception”. A fatal error will cause the script to stop executing at the point of the error. Since you caught the exception, it did what you told it to: Echo the string + error message, then it continued execution to “Hello World!”. If anything your error reporting is more verbose due to your INI settings, like the stack trace printout.