【问题标题】:php Exception.getMessage() always returns nothingphp Exception.getMessage() 总是什么都不返回
【发布时间】: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

标签: php exception


【解决方案1】:

电子贸易例外类是一团糟。它实现了自己的构造函数,并且没有为标准Exception 设置正确的值。它希望您使用$e-&gt;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;
    }

}

?>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      ["message":protected]=> string(0) "" 
      

      是你的问题

      get_class_methods($e)
      

      可能会暴露更多

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 2015-11-18
        • 2023-04-10
        • 2015-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多