PHP: 异常和可捕获的致命错误有什么区别?

PHP: What is the difference between an exception and a catchable fatal error?

我对这些术语及其在 PHP 中的确切含义/处理有些困惑:

Exception 可以这样定义:

When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

可以捕获并处理异常。

致命错误 可以这样定义:

Fatal errors are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

致命错误不一定能被捕获(它们不会抛出通常的异常),否则就没有更具体的Catchable Fatal Error.

然而,Catchable Fatal Error 与正常的 Exception[=35 有何不同=]?它的处理方式一样吗?可捕获的致命错误是否是特定类型的异常?

Fatal errors can not necessarily be caught (they do not throw usual exceptions)

在版本 7 之前,情况是这样的。用于阻止脚本停止运行的致命错误。但是,从版本 7 开始,它们现在显示为可捕获的异常。这使您可以从非常重要的问题中优雅地恢复。

However how is a Catchable Fatal Error different from a normal Exception?

他们都实现了 Throwable,但是锚点不同 类:

Throwable
    Error
        ParseError
        ...
    Exception
        RuntimeException
        ...

And is it handled the same?

是的,您可以捕获它们,就像异常一样。

Is a catchable fatal error a specific type of exception or not?

取决于你的语义。可捕捉的致命错误是一个例外,但它不是 Exception,如果你明白我的意思的话。你可以这样区分;

// "traditional" exceptions
try {
    throw new Foo();
} catch (Exception $e) {
}

// v7 catchable fatal errors
try {
    $not_an_object->not_a_method();
} catch (Error $e) {
}

// both
try { 
} catch (Throwable $e) {
}