为什么我的异常没有被捕获?
try {
\Account::destroy($id);
return Redirect::to("/manager/account")
->with("success_message", "Item excluido com sucesso");
} catch (Exception $e) {
return Redirect::to("/manager/account/{$id}/edit")
->with("error_message", "Erro ao excluir item");
}
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
imob_io
.users
, CONSTRAINTusers_account_id_foreign
FOREIGN KEY (account_id
) REFERENCESaccounts
(id
)) (SQL: delete fromaccounts
whereid
= 2)
最佳答案
当前您正在捕获当前命名空间内的类 Exception
。相反,您应该引用全局类型 \Exception
:
catch (\Exception $e){
return Redirect::to("/manager/account/{$id}/edit")
->with("error_message", "Erro ao excluir item");
}
我还建议您缩小范围,而不是只捕获每个异常。例如,您可以捕获 QueryException
,它将因违反约束等而被抛出。
catch(\Illuminate\Database\QueryException $e)
关于laravel - 为什么我的异常没有被 Laravel 的 destroy 方法捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27877312/