我收到错误代码为0的异常,但为什么不呢?异常的文本/消息是“在null上调用成员函数getId()”,但属性“代码”为0。为什么它为0而不是实数?所有其他异常均具有正确的错误代码。
是我的php.ini中的错误还是配置错误?
有人知道吗?
最佳答案
异常并不总是具有代码。如果未明确定义,则错误代码默认为0
。请参阅http://www.php.net/manual/en/exception.construct.php-异常将默认为""
消息,0
代码和先前的NULL
异常。
try {
throw new Exception('Hello!');
} catch(Exception $e) {
echo $e->getCode(); // prints 0
}
同样的事情,例如PHP错误:
try {
$foo = new stdClass;
$foo->fooBar();
} catch(Error $e) {
echo $e->getCode(); // prints 0 too
echo $e->getMessage(); // prints "Call to a member function fooBar() on null"
}
关于php - PHP 7.0.20:异常的代码为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46226278/