[英]Understanding try catch block
我對整個try catch塊感到困惑。 我理解如果拋出異常它執行catch塊,但是我有一個關於在try塊中使用return的問題。
try {
//other logic is here
//this is in laravel and sends the user back and should stop operation
if (foo != bar) {
return Redirect::back()->with_message('This auction is closed.', 'error');
}
} catch (Exception $e) {
return $e->getMessage();
}
所以我的問題是:在try塊內結束操作是否可以? 我是否會遇到一個錯誤,其中返回被忽略(或被視為異常?)並且代碼會繼續? 我再次對此非常陌生。
是的,這是編寫該代碼的完美有效方式。
在控制退出try
或catch
塊之后代碼將繼續運行的一種情況是在PHP 5.5中,它最終采用了來自其他語言的finally
塊,並且可以用於運行必須始終在try
塊之后執行的清理代碼,無論是否拋出異常。
但你可能不會使用5.5。
是的,沒關系。 如果由於任何原因Redirect :: back或with_message方法引發異常,您將返回錯誤消息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.