try / catch逻辑,条件代码块在try或者catch下面?

时间:2015-01-28 15:37:01

标签: php algorithm

我有一个简单的疑问,也许是一个新手,但是......看到这段代码:

try {
    $em->flush(); // this is Doctrine EntityManager#flush call

    [some code block]
    // some code here and as my business logic requires
    // if flush fails then this code shouldn't be executed
} catch (\Exception $e) {
    $e->getMessage());
}

现在,如果$em->flush()失败,PHP将尝试执行[some code block],否则它将直接转到catch?如果我不想在flush失败时执行代码,那么我应该退出try{} catch() {}句并将其放在下面?哪种方法正确?

1 个答案:

答案 0 :(得分:-1)

不知道你的追求是什么,但如果刷新功能是决定是否应该运行try / catch我会选择:

if($em->flush()){
  try {


  [some code block]
  // some code here and as my business logic requires
  // if flush fails then this code shouldn't be executed
  } catch (\Exception $e) {
    $e->getMessage());
  }
}