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

为什么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

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀