gpt4 book ai didi

php - 在 Laravel 中使用 try 和 catch 处理错误

转载 作者:IT王子 更新时间:2023-10-29 01:23:06 25 4
gpt4 key购买 nike

我想在我的应用程序中实现良好的错误处理,我已强制此文件用于捕获错误。

应用\服务\PayUService

try {
$this->buildXMLHeader; // Should be $this->buildXMLHeader();
} catch (Exception $e) {
return $e;
}

App\Controller\ProductController

function secTransaction(){
if ($e) {
return view('products.error', compact('e'));
}
}

这就是我得到的。

enter image description here

我不知道为什么 Laravel 没有将我重定向到 View 。错误是否强制正确?

最佳答案

您在 namespace 中,因此您应该使用 \Exception 来指定全局命名空间:

try {

$this->buildXMLHeader();

} catch (\Exception $e) {

return $e->getMessage();
}

在您的代码中,您使用了 catch (Exception $e) 所以 Exception 正在/as: 中搜索:

App\Services\PayUService\Exception

由于 App\Services\PayUService 内没有 Exception 类,所以它不会被触发。或者,您可以在类的顶部使用 use 语句,例如 use Exception; 然后您可以使用 catch (Exception $e) .

关于php - 在 Laravel 中使用 try 和 catch 处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444029/

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