PHP - 未捕获的SoapFault异常 - 如何忽略它并继续循环?

时间:2016-06-24 09:31:18

标签: php

我试图忽略错误并继续我的脚本,但我无法做到。这是我的代码:

try
{
    $client = new SoapClient('http://allegro.pl/uploader.php?wsdl');
    $version = $client->doQueryAllSysStatus(1, $allegro_user_webapi_key);
    $version = get_object_vars($version[0]);
    $session = $client->doLoginEnc($allegro_username, base64_encode(hash('sha256', $allegro_user_pass, true)), 1, $allegro_user_webapi_key, $version['ver-key']);

    $i = 0;
    $tab = array();
    while( (time() - $time) >= 240 )
    {
        $i++;

        $get = $client->doShowUser($allegro_user_webapi_key, 1, 15, ''); // HERE IS A PROBLEM
        $tab[] = $get['userLogin'];
    }   

    echo $i.'<br><br>'."\n\n";
    var_dump($tab);
}
catch(TemplateException $e)
{
    // continue
}

当我添加&#34; @&#34;这里:

$get = @$client->doShowUser($allegro_user_webapi_key, 1, 15, '');

它不起作用......当我删除尝试并抓住它时也不能正常工作。我怎么能肯定地忽略这些错误并继续我的脚本?

感谢。

2 个答案:

答案 0 :(得分:0)

在您的尝试和捕获中,您将捕获TemplateException类型的异常。

这不会捕获抛出的异常,因为它是SoapFaultException

try {
  ...
} catch (TemplateException $e) {
  // do something when a TemplateException is thrown

} catch (SoapFaultException $e) {
  // do something when a SoapFaultException is thrown

} catch (Exception $e) {
  // catch any other exception that is thrown
} 

按顺序处理try catch(TemplateException&gt; SoapFaultException&gt; ..)

如果你只想抓住SoapFaultException,你可以这样做:

try {
  ...
} catch (SoapFaultException $e) {
  // do something when a SoapFaultException is thrown

} 

如果您想捕获所有异常,无论其类型如何:

try {
  ...
} catch (Exception $e) {
  // catch all exceptions that are thrown
}

如果您只想捕获由doShowUser触发的异常并仍然继续while循环,那么您需要移动try catch

while( (time() - $time) >= 240 )
{
    try {
       $i++;

       // HERE IS A PROBLEM
       $get = $client->doShowUser($allegro_user_webapi_key, 1, 15, '');
       $tab[] = $get['userLogin'];

    } catch (Exception $e) {
       // do nothing
    }        

}  

答案 1 :(得分:0)

将try catch放在循环中

while( (time() - $time) <= 240 )
   {
   $i++;
   try{
      $get = $client->doShowUser($allegro_user_webapi_key, 1, 15, ''); // HERE IS A PROBLEM
   }
   catch(Exception $e)
   {
      // do smthing
   }
    $tab[] = $get['userLogin'];
}