Home » Php » php – Can't catch symfony FatalErrorException

php – Can't catch symfony FatalErrorException

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have code like this:

try {
    $var = $object->getCollection()->first()->getItem()->getName();
} catch(\Exception $e) {
    $var = null;
}

Of course i have communicative variable and method names. This is just demonstration.

So if my collection is empty the Collection::first() will return false. Then the getItem call will throw a Symfony\Component\Debug\Exception\FatalErrorException which won’t be catched by the code above.

My question is that how can i catch this exception? I have long chains like this with many getters that can return null. So i prefer this way rather than checking every value for null.

How to&Answers:

Use Throwable class instead Exception class:

try {
    $var = $object->getCollection()->first()->getItem()->getName();
} catch(\Throwable $e) {
    $var = null;
    $msg = $e->getMessage();
}

Since PHP 7.0 exceptions thrown from fatal and recoverable errors are instances of a new and separate exception class: Error. This new Error class implements Throwable interface, which specifies methods nearly identical to those of Exception. Because Throwable is higher in hierarchy you can catch with it both, \Error and \Exception.

interface Throwable
|- Exception implements Throwable
    |- ...
|- Error implements Throwable
    |- TypeError extends Error
    |- ParseError extends Error
    |- ArithmeticError extends Error
        |- DivisionByZeroError extends ArithmeticError
    |- AssertionError extends Error

Answer:

As you can see here, FatalErrorException extends ErrorException (PHP) that extends itself php Exception class.

Now that you have all this elements, you’re ready for next step: as the name of exception says, this is a FatalError (a concept related to PHP and not with Symfony2; in that case they built a wrapper class for this error, maybe for interface purposes).

A PHP fatal error isn’t a catchable one so is pretty useless to keep the code that could cause the FatalError, inside a try ... catch block

You should check, as a common and good rule, whenever is possible for returned values before try to access them.

Update

Since I’ve seen an upvote to my answer after PHP7 was released, I would like to caveat that since PHP7 is possible to catch fatal errors so this answer is still valid but only for PHP versions < 7.

Answer:

Ok. I’ve found a workaround. I use the property accessor component which throws simple exceptions, not fatal errors.

$pa = \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor();
try {
    $var = $pa->getValue($object, 'collection[0].item.name');
} catch(\Exception $e) {
    $var = null;
}

Answer:

Works for me (PHP 7.0, Symfony 3.0.9):

use Symfony\Component\Debug\Exception\FatalThrowableError;
...
try {
    throw new FatalErrorException("something happened!", 0, 1, __FILE__, __LINE__);
} catch (FatalErrorException $e) {
    echo "Caught exception of class: " . get_class($e) . PHP_EOL;
}

Output:

Caught exception of class: Symfony\Component\Debug\Exception\FatalErrorException

Answer:

Can you try this:

$foo = $object->getCollection()->first();
if($foo){
  $var = $foo->getItem()->getName();
} else {
    // fallback
}