The question is a continuation of the problem described here .

I am writing an error handler class in PHP7. Install error handlers:

set_error_handler([$this, 'errorHandler']); set_exception_handler([$this, 'exceptionHandler']); ob_start(); register_shutdown_function([$this, 'fatalErrorHandler']); 

The handlers themselves:

 public function fatalErrorHandler() { ob_clean(); $error = error_get_last(); if ( !empty($error) && $this->isFatalError($error['type']) ) { throw new ErrorException($error['message'], $error['type']); } throw new ErrorException('Error'); } 

and

 public function exceptionHandler(\Throwable $e) { $this->error = [ 'code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]; $this->displayError(); $this->log(); } 

The ErrorException class is a descendant of Exception β€” the descendant of the base \ Exception. There added a couple of methods.

The error itself: enter image description here

It turns out, PHP just complains about the string included in the constructor by the first argument. Similarly, if you throw the base \ Exception. What is the problem?

    2 answers 2

    Documentation register_shutdown_function

    Registers the callback function, which will be executed after the script is completed or when the exit () function is called.

    Those. Everything, the script's work is completed, there is no one to catch the exception.

      From the documentation :

      In PHP 7, most internal errors now generate an exception, but of the Error class. For them, the specified exception handler will also be called. The Error and Exception classes implement the Throwable interface, so it should be used in the handler function signature.

      The problem was different:

      In my situation, in principle, the exceptionHandler first worked, in which the Error object came. And I thought it was marveled by the fatalErrorHandler .