[英]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.