我想在我的应用程序中实现一个好的错误处理,我已经强制这个文件来捕获错误.

App\Services\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'));
  }
}

And this is what I get.

enter image description here

I don't know why Laravel is not redirecting me to the view. Is the error forced right?

推荐答案

You are inside a namespace so you should use \Exception to specify the global namespace:

try {

  $this->buildXMLHeader();

} catch (\Exception $e) {

    return $e->getMessage();
}

In your code you've used catch (Exception $e) so Exception is being searched in/as:

App\Services\PayUService\Exception

因为App\Services\PayUService里面没有Exception类,所以它不会被触发.或者,你可以在类的顶部使用use语句,比如use Exception;,然后使用catch (Exception $e).

Laravel相关问答推荐

到查询构建器的MySQL查询

如何在 Laravel 中使用 return 停止 Trait php 的执行

阻止用户查看其他用户正在查看的内容的最佳方法是什么?

laravel 如何验证跨越午夜的两个值之间的时间(15:00 > 时间 > 01:00)

使用 laravel 根据当月显示注册用户列表

如何在 Laravel 中填充以后执行的条件

Laravel Homestead vagrant up 超时

如何在 NetBeans 中添加带有点 (blade.php) 的自定义文件扩展名?

Laravel Session 总是改变 Laravel 5.4 中的每个刷新/请求

Laravel 将 JSON 转换为数组?

file_put_content ...无法打开流:Laravel 5 中的权限被拒绝

Laravel 生产问题 - 使用 Laravel 4.1.x 更新composer

约定表名(带下划线)

在共享主机中仅使用 FTP 部署 Laravel 5

Laravel Eloquent ORM 复制

如何运行artisan命令计划:在托管服务器上运行? (Laravel )

无法声明类 Controller,因为该名称已在使用中

Laravel 5 - 跳过迁移

Laravel 4 - 文件上传

在 Laravel 5 中找不到类App\Http\Controllers\admin\Auth