繁体   English   中英

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

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

我有一个简单的疑问,也许是一个新手,但是...请参见以下代码:

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() {}语句,然后放在下面吗? 哪种方法正确?

不知道您的事之后,但是如果冲洗功能是要确定是否应运行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