简体   繁体   English

try / catch逻辑,try或以下catch中的条件代码块?

[英]try/catch logic, conditional code block inside try or below catch?

I have a simple doubt, perhaps a newbie one, but ... See this code: 我有一个简单的疑问,也许是一个新手,但是...请参见以下代码:

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());
}

Now, if $em->flush() fails, PHP will try to execute [some code block] or it will goes directly to catch ? 现在,如果$em->flush()失败,PHP将尝试执行[some code block] ,否则将直接catch If I don't want the code to be executed if flush fails then I should get out of try{} catch() {} sentence and put it below? 如果我不希望在flush失败时执行代码,那么我应该摆脱try{} catch() {}语句,然后放在下面吗? Which is the right way? 哪种方法正确?

Not sure what your after but if the flush function is to decide if the try/catch should be run i would go for: 不知道您的事之后,但是如果冲洗功能是要确定是否应运行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());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM