【问题标题】:How continue php script execution on exception in Zend Framework?如何在 Zend Framework 中的异常上继续执行 php 脚本?
【发布时间】:2013-02-07 22:27:12
【问题描述】:

当我执行我的脚本时出现问题并引发异常,但不是停止所有脚本。如何告诉 zend 继续?

当我获取邮件时出现此错误我有一个 try catch 块但它没有捕获。

Fatal error: Uncaught exception 'Zend\Mail\Exception\RuntimeException' with message 'Line "X-Assp-Message/IP-Score: 

谢谢。

我的代码是一个获取邮件的简单类:

$listm  = new Zend\Mail\Storage\Pop3(array('host' => $this->mServer,'user' => $this->mMail, 'password' => $this->mPassword));

foreach ($listm as $msgp3)
{
    try 
    {
        e($msgp3->from);
        e($msgp3->to);
        e($msgp3->subject);
        e($msgp3->date);
        e(strtotime($msgp3->date));
        e($msgp3->messageid);
    } catch (Exception $e) {
        e($e->getMessage());
    }
}

而我的代码在 10em 邮件处停止,那么如何让 Zend 不停止呢?

【问题讨论】:

  • 如果你能贴出你的代码会更好看。
  • "我有一个 try catch 块,但它没有捕获。" - 那么你做错了什么。

标签: php zend-framework exception uncaught-exception


【解决方案1】:

我终于发现问题出在哪里了:

当我在这里获取消息时返回错误,因此在 for 指令中:

foreach ($listm **as $msgp3**)

要在获取消息时捕获任何错误,我必须以这种方式获取:

$maxMessage = count($messageList);
        for($i = 0; $i < $maxMessage; $i++) 
        {
            try{
                $msgp3 =  $messageList->getMessage($i);                 
                //--- WORK ON msgp3
                }catch(Exception $e) {
                echo 'E2->'.$e->getMessage();
            }
        }

现在我的脚本继续...

【讨论】:

    【解决方案2】:

    你是如何发现异常的?您能否提供问题中的 try/catch 代码?

    在 Zend 中,您需要使用被抛出的完整 zend 异常类。在这种情况下,它是Zend\Mail\Exception\RuntimeException,它变成了Zend_Mail_Exception_RuntimeException

    try
    {
      // ...
    }
    catch (Zend_Mail_Exception_RuntimeException $e)
    {
      // ...
    }
    

    【讨论】:

      【解决方案3】:

      如果您不想在指出异常时停止进程。您可以使用 try and catch 方法。像这样:

       try {
          DoSomethingReallyBad()
       }
       catch(RuntimeException $e) {
          // do nothing
       }
      
       // go further
      

      我必须说何时调用异常。您上一个任务的进程已退出。

      注意:我没有测试这个!

      【讨论】:

      • 事实上异常是真的捕获因为我可以打印 $e->getMessage();但真正的问题是脚本停在这里。
      【解决方案4】:

      异常的目的是告诉您发生了不好的事情,您需要构建代码来正确处理它。没有看到你的代码,虽然调试起来有点困难。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多