Can't catch symfony FatalErrorException

后端 未结 4 470
感情败类
感情败类 2020-12-29 19:29

I have code like this:

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


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 19:52

    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
    

提交回复
热议问题