【发布时间】:2018-07-11 08:53:58
【问题描述】:
我有一个问题:如何在 PHP 中抛出一个 NullPointer 类型的新异常?
我想做这样的事情(在我的 TryException.php 文件中):
public function getValue($stringKey) {
if($this->result[$yellow] !== NULL)
{
return $this->result[$yellow];
}
else
{
throw new NullPointerException("The \"$yellow\" does not exist");
}
}
但是当我捕获 NullPointerException(在 main.php 文件中)时,我不能这样做(它不在 catch 语句中):
try
{
$config->getValue('arcshive')
echo 'ok';
}
catch (NullPointerException $e)
{
echo $e->getMessage();
}
如果我抛出一个新异常(并且我捕获它)(不是 NullPointer),它会正常工作。
我该怎么办?
【问题讨论】:
-
@B001ᛦ 我编辑了问题
标签: php exception exception-handling nullpointerexception try-catch