关于php:Laravel中尝试捕获的错误处理

Error handling with try and catch in Laravel

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

App \\\\\\\\\\ Services \\\\\\\\ PayUService

1
2
3
4
5
try {
  $this->buildXMLHeader; // Should be $this->buildXMLHeader();
} catch (Exception $e) {
        return $e;
}

App \\\\\\\\ Controller \\\\\\\\ ProductController

1
2
3
4
5
function secTransaction(){
  if ($e) {
    return view('products.error', compact('e'));
  }
}

这就是我得到的。

enter

1
2
3
4
5
6
7
8
try {

  $this->buildXMLHeader();

} catch (\\Exception $e) {

    return $e->getMessage();
}

在您的代码中,您已经使用过catch (Exception $e),因此正在/ as中搜索Exception

1
App\\Services\\PayUService\\Exception

由于App\\Services\\PayUService内部没有任何Exception类,因此不会被触发。另外,您可以在类的顶部使用use语句,例如use Exception;,然后可以使用catch (Exception $e)