gpt4 book ai didi

php - LARAVEL -> 为什么 try catch 在 Laravel 中不起作用?

转载 作者:可可西里 更新时间:2023-11-01 00:13:13 26 4
gpt4 key购买 nike

我在 RouteServiceProvider 中有代码:

$router->bind('user', function ($value) {
try{
throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
}catch(Exception $e){
exit('nott');
}
});

我没有得到输出

nott

我得到了

Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteServiceProvider.php line 75:
...

编辑:这有效:

$router->bind('user', function ($value) {
try{
throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
}catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
exit('addd');
}
});

但这行不通:

$router->bind('user', function ($value) {
try{
return (new User)->findOrFail(122);
}catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
exit('addd');
}
});

最佳答案

$router->bind('user', function ($value) {
try{
throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
}catch(\Exception $e){
exit('nott');
}
});

use Exception; //on top

$router->bind('user', function ($value) {
try{
throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
}catch(Exception $e){
exit('nott');
}
});

我想现在你明白你错过了什么。

关于php - LARAVEL -> 为什么 try catch 在 Laravel 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com