11

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]

igorsantos07
  • 4,456
  • 5
  • 43
  • 62
  • 1
    php errors are still a thing. warnings and notices are still here. – Federkun Nov 01 '16 at 13:46
  • It is a common misconception that all errors are exceptions in PHP 7. Yes PHP 7 now throws more exceptions than before, but not all errors have turned into exceptions. – apokryfos Nov 01 '16 at 13:49
  • You could actually define `set_error_handler()` only, since uncaught exceptions will be converted to fatal errors. – simon Nov 01 '16 at 13:59

3 Answers3

8

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.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • this is an interesting addition (as it explains what became `Error`) but still doesn't answer directly the question. It means if I define `set_exception_handler()` on PHP7 I don't need to define the error handler as well, as everything will be routed into the exception handler? – igorsantos07 Nov 01 '16 at 14:48
  • You can do it that way, yes. `Throwable` will catch `Error` and `Exception` – Machavity Nov 01 '16 at 15:28
3

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();
 }
Community
  • 1
  • 1
apokryfos
  • 38,771
  • 9
  • 70
  • 114
1

You can use php trigger_error('test error') to see what happen when error is not handled by php set_exception_handler()