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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类