简体   繁体   中英

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 ? 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? 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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