php Exception.getMessage()总是不返回

时间:2015-08-21 18:47:53

标签: php exception

当我在php中捕获异常并尝试输出一些细节时,getMessage()总是不返回任何内容。如果我执行var_dump(),我会看到要显示的消息。我做错了什么?

                try
                {
                    ...
                }
                catch (Exception $e)
                {
                    echo "<p>Exception: " . $e->getMessage() . "</p>"; 
                    return;
                }

如果我执行var_dump($ e),我会得到以下输出:

  

object(ETWSException)#735(10){[&#34; errorCode&#34;:protected] =&gt; INT(401)   [&#34;的errorMessage&#34;:保护] =&GT;字符串(226)&#34; 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&#34;   [&#34; httpCode&#34;:保护] =&GT; NULL [&#34; message&#34;:protected] =&gt; string(0)&#34;&#34;   [&#34;串&#34;:&#34;例外&#34;:私人] =&GT; string(0)&#34;&#34; [&#34;代码&#34;:保护] =&GT;   int(0)[&#34; file&#34;:protected] =&gt; [snip!]

我认为getMessage()应该显示errorMessage的内容。

我尝试了$ e-&gt; getErrorMessage()和 显示预期的消息。搜索谷歌的php异常getErrorMessage似乎没有显示任何有用的东西(所有页面似乎只提到getMessage,而不是getErrorMessage)。是什么给了什么?

3 个答案:

答案 0 :(得分:6)

电子贸易例外类是一团糟。它实现了自己的构造函数,并没有为标准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;
    }

}

?>

答案 1 :(得分:1)

这里有几个问题。首先,如果查看$ e的var_dump,则消息索引为空。因此,当您使用getMessage时,您将无法获得任何回报。其次,抛出的异常不是标准的PHP异常。它由您正在使用的API编写,您需要阅读其文档以了解如何正确处理异常。

答案 2 :(得分:0)

["message":protected]=> string(0) "" 

你的问题是

get_class_methods($e)

可能会暴露更多

相关问题