dongyong6428 2014-12-16 07:28
浏览 12
已采纳

PHP:即使发现异常,如何执行try块?

I have a try block in which some exceptions will be definitely thrown but I don't want to stop the execution of the try block if this happens.

This is the code:

try {
    $queryStr = "SELECT * FROM `$current_table` WHERE ...";
    $query = $db->prepare($queryStr);
    $query->execute();

    while($row = $query->fetch()) {

    }

} catch(PDOException $e) {
    //echo $e->getMessage();
}

This try/catch block is inside a for loop, so $current_table will be changing continously. Now this current table can take 4 values april-june-2014, july-september-2014, october-december-2014 and jan-march-2015. These table names are stored in an array.

Now when it will query the first table and if it is found, then it's well and good but if it is not found then I want to check the other tables and see if those exists. If any of the table is not found, it will throw an exception and then it won't check the other tables.

Is there a way to do that?

  • 写回答

5条回答 默认 最新

  • douqian2957 2014-12-16 15:48
    关注

    I actually had some structure problems in tables so because of that I was unable to get data from them. But this code is working fine. The remaining code in try block will be executed even if there is an exception. So there is no need to add anything else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效