如何访问 PHP 异常的内部异常

How to access a PHP Exception's Inner Exception

当我创建 __soapCall(客户端指向 WSDL)并且请求在服务器端无效时,捕获到的异常消息一般如下所示:

An error occurred while executing the command definition. See the inner exception for details.

这是我的 try-catch 块之一的示例:

try {
    $soapCallResult = $client->myWSDLMethod(array(...));
} catch (Exception $e) {
    echo 'Exception in myWSDLMethod: ',  $e->getMessage(), PHP_EOL;
}

当我 var_dump getTrace(), the array only points to the file and line where I make this call, which is not useful at all... It seems there is no getInnerExceptionMessage() method in the Exception class 或类似的东西时。那么我应该如何访问该内部异常?

如果您 var_dump($e->detail),您可以看到整个异常的 详细信息 对象并通过以下路径访问内部异常的消息:

$e->detail->ExceptionDetail->InnerException->Message

您可能有兴趣从此对象打印更多字段。这是转储:

object(stdClass)[?]
  public 'ExceptionDetail' => 
    object(stdClass)[?]
      public 'HelpLink' => null
      public 'InnerException' => 
        object(stdClass)[?]
          public 'HelpLink' => null
          public 'InnerException' => null
          public 'Message' => string 'SERVER ERROR MSG: ...' (length=?)
          public 'StackTrace' => string '   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)'... (length=?)
          public 'Type' => string 'System.Data.SqlClient.SqlException' (length=34)
      public 'Message' => string 'An error occurred while executing the command definition. See the inner exception for details.' (length=94)
      ...

希望对您有所帮助。