douzhi2012
2018-04-16 04:01
浏览 144
已采纳

为什么PHP不能捕获在尝试实例化SoapClient对象时导致的异常?

If you dont have the php_soap.dll extension enabled in the php.ini file, trying to create an instance of SoapClient will cause PHP to break.

If i surround the instatiation with a try-catch block like so,

try{
       $client = new SoapClient ($wsdl, array('cache_wsdl' => WSDL_CACHE_NONE) );   
       $result = $client->{$web_service}($parameters)->{$web_service."Result"};
       return $result;
   }
   catch(Exception $e){
        echo $e->getMessage();
   }

it wont catch the excetion. Instead, it is like die()has been called somewhere in the internal PHP code. Does anyone know why this is the case?

note: im using PHP version 7.2.1

图片转代码服务由CSDN问答提供 功能建议

如果您没有在php.ini文件中启用php_soap.dll扩展名,请尝试创建<的实例 代码> SoapClient 将导致PHP中断。

如果我用类似try-catch的块包围instatiation,

 <  code> try {
 $ client = new SoapClient($ wsdl,array('cache_wsdl'=&gt; WSDL_CACHE_NONE));  
 $ result = $ client-&gt; {$ web_service}($ parameters) - &gt; {$ web_service。“Result”}; 
返回$ result; 
} 
 catch(Exception $ e){
  echo $ e-&gt; getMessage(); 
} 
   
 
 

它不会捕获除外。 相反,它就像在内部PHP代码中的某处调用了 die()。 有谁知道为什么会这样?

注意:即时通讯使用PHP版本7.2.1

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanji6997 2018-04-16 06:25
    已采纳

    As Arvind already pointed out, Errors and Exceptions are two different things in PHP. try/catch works only with Exceptions, not with errors.

    Here a little bit more explanation what you can do:

    Error handling is defined more or less globally for an application. The built-in error handling checks the severity of the error and depending on this, logs the error and halts the execution.

    You can override this behavior by setting a custom error handler using set_error_handler() (http://php.net/set_error_handler).

    Quite a common way is to define an custom error handler which raises an Exception. Then you're able to handle errors and exceptions with try/catch in your code. An example error handler which does exactly this is written here: http://php.net/manual/en/class.errorexception.php.

    Copied from there the most interessting part:

    function exception_error_handler($severity, $message, $file, $line) {
        if (!(error_reporting() & $severity)) {
            // This error code is not included in error_reporting
            return;
        }
        throw new ErrorException($message, 0, $severity, $file, $line);
    }
    set_error_handler("exception_error_handler");
    

    If you place this code somewhere near to the start of your application, instead of raising errors ErrorExceptions will be thrown. This should work with the SoapClient errors.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题