What happens with set_error_handler() on PHP7 now that all errors are exceptions?

天大地大妈咪最大 提交于 2019-12-21 04:39:18

问题


On PHP5 it makes a whole lot of sense having both set_exception_handler() and set_error_handler() defined.

However, on PHP7 all (most?) errors are now exceptions. So, what's the point on defining both handlers, if even errors would pass by the exception handler instead?

I see there's a note on PHP7 new Error class in the exception handler doc, but there's no reference to the fact there's no plain errors anymore, but Throwables, in the error handler function.

Since PHP 7, most errors are reported by throwing Error exceptions, which will be caught by the handler as well. Both Error and Exception implements the Throwable interface. [source]


回答1:


Aaron Piotrowski (the guy who made the new Error-Exception system) has a great blog on this. I think the key point you need to understand is this

In PHP 7, an exception will be thrown when a fatal and recoverable error (E_ERROR and E_RECOVERABLE_ERROR) occurs, rather than halting script execution. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately halting script execution. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5.x goes uncaught, it will still be a fatal error in PHP 7.

Note that other types of errors such as warnings and notices remain unchanged in PHP 7. Only fatal and recoverable errors throw exceptions.

To put this a different way consider this

  • set_exception_handler() - Function to handle Exceptions by default (as of PHP 7.0 this can handle all Throwables, so it can catch recoverable errors)
  • set_error_handler() - Function to handle recoverable errors

In other words, their functionality didn't change. Anything that triggers them in PHP5 will trigger them in PHP7, it's just that, now, you can use a try-catch block at the script level to handle a specific error.




回答2:


http://php.net/manual/en/language.errors.php7.php is a good read on this:

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

As with normal exceptions, these Error exceptions will bubble up until they reach the first matching catch block. If there are no matching blocks, then any default exception handler installed with set_exception_handler() will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a traditional error.

This means that errors are not technically exceptions, however they can be caught like exceptions (which is a nice feature).

For example the following should work as before:

 set_error_handler('handleError'); 
 try {
    // raise error          
 } catch (Exception $e) {
    // won't catch error 
 }

However the following should also be possible

 try {
    // raise error          
 } catch (Exception $e) {
    // won't catch error 
 } catch (Error $e) {
      handleError();
 }


来源:https://stackoverflow.com/questions/40361353/what-happens-with-set-error-handler-on-php7-now-that-all-errors-are-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!