0

I had an issue that ultimately boiled down to not checking that a value was false before proceeding.

I tried to put a try/catch around it for debugging, but strangely that didn't help.

Here's a minimal example:

    try
    {
        $test = false;
        $test->format('Y-m-d');
    }

    catch (\Exception $e)
    {

    }

The error log shows that it's a fatal error. Does PHP have documentation on why this wouldn't throw a normal error?

Pikamander2
  • 7,332
  • 3
  • 48
  • 69

1 Answers1

2

It does trigger "normal" error, but Errors aren't instance of Exception, they are separate branch of objects. Common ancestor for Exception and Error is Throwable and you might want to use that.

Your catch block should look like

catch (\Throwable $e)
{
    // do stuff
}

or if you want to catch only Errors

catch (\Error $e)
{
    // do stuff
}

Also note that prior to PHP7 Errors and Exceptions are two different things and are handler in different way. Errors can't be caught using try-catch block, you have to use error handler, check set_error_handler

mleko
  • 11,650
  • 6
  • 50
  • 71