【发布时间】:2015-08-21 18:47:52
【问题描述】:
当我在 php 中捕获异常并尝试输出一些详细信息时,getMessage() 总是不返回任何内容。如果我执行 var_dump(),我会看到我想要显示的消息。我做错了什么?
try
{
...
}
catch (Exception $e)
{
echo "<p>Exception: " . $e->getMessage() . "</p>";
return;
}
如果我执行 var_dump($e),我会得到以下输出:
object(ETWSException)#735 (10) { ["errorCode":protected]=> int(401) ["errorMessage":protected]=> string(226) "HTTP/1.1 401 未经授权 日期:2015 年 8 月 21 日星期五 18:26:30 GMT 服务器:Apache WWW-Authenticate: OAuth 领域=https://etws.etrade.com/,oauth_problem=token_expired 内容长度:995 内容类型:text/html;charset=utf-8 " ["httpCode":protected]=> NULL ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=>[snip!]
我认为 getMessage() 应该显示 errorMessage 的内容。
好吧,我尝试了 $e->getErrorMessage() 并且 that 显示了预期的消息。在谷歌搜索 php 异常 getErrorMessage 似乎没有显示任何有用的信息(所有页面似乎只提到 getMessage,而不是 getErrorMessage)。什么给了?
【问题讨论】:
-
文档显示 getMessage()...php.net/manual/en/exception.getmessage.php 这很奇怪。
-
我知道...我想我要疯了。
-
你能做一个
php -v吗?我在我的盒子(5.6.12)上进行了测试,getMessage 是正确的函数名称。 -
所以你在这里得到了一个
ETWSException类型的对象——很可能这是一个扩展 PHP 异常类的类。去看看你在那里使用什么的文档,或者查看源代码,看看它提供了哪些属性和方法…… -
@CBroe 可能是正确的。这是一个测试,表明 getMessage 是所有最新 PHP 版本中 Exception 的正确函数:3v4l.org/hUkjb