2017-09-15 35 views
4

我看到这个代码在PHP documentation什么是异常严重的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); 
} 

它继续:

This exception severity is: 256 
bool(true) 

什么异常的严重性意味着,和我必须在所有使用它呢?

回答

2

$severity是表示,那么,所述错误的严重性抛出的整数。该手册指出,它可以是任何整数,但它是优选使用恒定从predefined error constants。这些与error_reporting使用的相同。

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

因此,ErrorException::$severity实际上是严重程度的PHP错误,如果您没有将其作为Exception抛出,则会显示该错误。你可以用它来决定做什么,当你发现一个ErrorException取决于什么原因造成的。