Php throw exception thrown but error message still appears

I am using some php reference code to create an exception test, but getting some weird message.

here 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 is the conclusion:

 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 is strange that although the exception was caught, the exception message is still included ...

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) . This is the xdebug extension that led to such error output. By default, xdebug shows stack traces under error conditions. For those who want to disable it, you can run the command:

 xdebug_disable();//put it on the header of your code,like cofig file. 

more details

+4
source share
4 answers

When you try / catch () a block of code, then you will not get the fatal "Uncaught exception" error. A fatal error will cause the script to stop running at the point of error. Since you caught the exception, he did what you told him: Echo line message + error message, then it continued executing "Hello World!". If something is your error message in more detail because of your INI settings, for example, printing a stack trace.

+3
source

If you do not want to disable xdebug and do not want the trace message to be displayed, you can disable the display of the exception trace using

 xdebug.show_exception_trace = 0 

in php.ini

+3
source

If you do not want errors to be displayed, disable the error_reporting and display_error functions. Then it will only show you a message stating that the exception is being printed

+1
source

Setting display_errors = off will give you the behavior you want, but remember that you will not receive ANY error messages, and if you are not in a production environment, you are strongly advised to leave this to.

If you do not want to disable error reporting for all php, you can do one of them:

  • set <?php ini_set('display_errors', 0); ?> <?php ini_set('display_errors', 0); ?> at the top of your script.
  • create a virtual host for your site (if you don’t already have one) and create .htaccess and enable the php_flag display_errors "1"

hope this helps

0
source

Source: https://habr.com/ru/post/1390633/


All Articles