当我在php中捕获异常并尝试输出一些细节时,getMessage()总是不返回任何内容.如果我执行var_dump(),我会看到要显示的消息.我究竟做错了什么?
try
{
...
}
catch (Exception $e)
{
echo "<p>Exception: " . $e->getMessage() . "</p>";
return;
}
Run Code Online (Sandbox Code Playgroud)
如果我做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 realm = https://etws.etrade.com/,oauth_problem=token_expired 内容长度:995内容类型:text/html; charset = utf-8"[" httpCode":protected] => NULL ["message":protected] => string(0)""["string":"异常":private] => string(0)""["code":protected] = > int(0)["file":protected] => [snip!]
我认为getMessage()应该显示errorMessage的内容.
嗯,我试过$ E-> getErrorMessage()和这显示预期的消息.搜索谷歌的php异常getErrorMessage似乎没有显示任何有用的东西(所有页面似乎只提到getMessage,而不是getErrorMessage).是什么赋予了?
电子贸易例外类是一团糟.它实现了自己的构造函数,并没有为标准设置正确的值Exception.它希望您用来$e->getErrorMessage()获取消息.
<?php
/**
* E*TRADE PHP SDK
*
* @package PHP-SDK
* @version 1.1
* @copyright Copyright (c) 2012 E*TRADE FINANCIAL Corp.
*
*/
class ETWSException extends Exception
{
protected $errorCode;
protected $errorMessage;
protected $httpCode;
/**
* Constructor ETWSException
*
*/
public function __construct($errorMessage, $errorCode = null, $httpCode = null, Exception $previous = null) {
$this->errorMessage = $errorMessage;
$this->errorCode = $errorCode;
$this->httpCode = $httpCode;
}
/**
* Gets the value of the errorCode property.
*
* @return
* possible object is
* {@link Integer }
*
*/
public function getErrorCode() {
return $this->errorCode;
}
/**
* Gets the value of the errorMessage property.
*
* @return
* possible object is
* {@link String }
*
*/
public function getErrorMessage() {
return $this->errorMessage;
}
/**
* Gets the value of the httpStatusCode property.
*
* @return
* possible object is
* {@link String }
*
*/
public function getHttpCode() {
return $this->httpCode;
}
}
?>
Run Code Online (Sandbox Code Playgroud)