Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exception is caught, but error message still appears

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.

more details

like image 275
zhkzyth Avatar asked Jan 12 '12 12:01

zhkzyth


2 Answers

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.

like image 186
Derek Wright Avatar answered Nov 15 '22 06:11

Derek Wright


If you do not want to disable xdebug and you do not want the trace message to appear, you can turn off the display of the exception trace with

xdebug.show_exception_trace = 0

in your php.ini

like image 39
Baster Avatar answered Nov 15 '22 08:11

Baster