Recoverable Fatal Errors

Recoverable fatal errors – sometimes also called catchable fatal errors – are an error level used for cases where the engine decided that something is wrong, but did not end up in an unstable state. In other words: it is unlikely that the application should continue as if nothing had happened but from the engine’s perspective there is still a chance to recover.

The most common scenario is a mismatch on the type of the parameter as required by the method’s signature and the type of the actual argument when the method got called:

class Example
{
    public function method(stdClass $c)
    {
       // ...
    }
}

$x = new Example();
$x->method('not-a-stdclass-instance');

If this error is not caught by a user defined handler as described above, PHP 5 aborts the execution:

PHP Catchable fatal error:  Argument 1 passed to Example::method() must be an
instance of stdClass, string given, called in ...

With the introduction of the engine exceptions in PHP 7, this error condition now can be caught with a standard catch block. Given the same class definition as above, this could look like this:

try {
    $x = new Example();
    $x->method('not-a-stdclass-instance');
} catch (Error $error) {
    var_dump($error);
}
Object(TypeError)#2 (7) {
  ["message":protected]=>
  string(121) "Argument 1 passed to Example::method() must be ..."
  ...