PHP 中的异常严重性是什么?

Mic*_*aby 5 php error-handling

我在PHP 文档中看到了这段代码:

try {
throw new ErrorException("Exception message", 0, E_USER_ERROR);
} catch(ErrorException $e) {
echo "This exception severity is: " . $e->getSeverity();
var_dump($e->getSeverity() === E_USER_ERROR);
}
Run Code Online (Sandbox Code Playgroud)

它继续:

This exception severity is: 256
bool(true)
Run Code Online (Sandbox Code Playgroud)

异常严重性是什么意思,我是否必须使用它?

ish*_*egg 3

$severity一个整数,表示抛出的错误的严重性。手册指出它可以是任何整数,但最好使用预定义误差常量中的常量。这些与error_reporting使用的相同。

请注意,ErrorException扩展了Exception,添加了$severity参数。这是因为ErrorException通常用于将 PHP 显示的正常错误转换为Exceptions。这是通过set_error_handler()完成的。

因此,如果您没有将 PHP 错误ErrorException::$severity作为. 您可以使用它来决定当您捕捉到时要做什么,具体取决于引起它的原因。ExceptionErrorException