为什么我的异常没有被 Laravel destroy 方法捕获?
Why is my exception not caught by the Laravel destroy method?
为什么我的异常没有被捕获?
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
, CONSTRAINT users_account_id_foreign
FOREIGN KEY
(account_id
) REFERENCES accounts
(id
)) (SQL: delete from
accounts
where id
= 2)
目前您正在捕获当前命名空间内的 class 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)
为什么我的异常没有被捕获?
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)
目前您正在捕获当前命名空间内的 class 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)