在Cakephp 3中捕获致命错误

I'm trying to catch error when invalid data to imagecreatefromjpeg are passed. Cake php displays error page saying Fatal Error Cake\Error\FatalErrorException so this code supposed to work but it is not:

            try {
                $src_img = imagecreatefromjpeg($image);
            } catch (\Cake\Error\FatalErrorException $e) {
                echo 'Caught exception: ',  $e->getMessage(), "
";
            }

I'm also tried to use \Exception, \Cake\Core\Exception\Exception, \ErrorException but with no success.

imagecreatefromjpeg() normally shouldn't cause fatal errors, but only warnings, you may want to investigate that further.

Anyhow, catching fatal errors via try...catch is only possible as of PHP 7, where most of them have been changed to exceptions. You'd have to catch \Error or \Throwable in that case. However there are still fatal errors that cannot be catched, for example when require() fails, or memory is exceeded.

\Cake\Error\FatalErrorException is being created internally in a regular error handler, where (uncatched) fatal errors will be handled, ie that exception is not being thrown, and therefore cannot be catched.

See also