在我的Laravel 5.6应用程序中,使用 throw $e 似乎直接将异常输出到屏幕并绕过Handler.php异常处理程序 . 这意味着在开发过程中,whoops和 生产环境 中不会显示异常,也不会报告异常 .

当我写这篇文章时,我意识到我可能一直在使用try / catches,但也许有人可以证实这一点?

我目前的代码:

try{
        $stockLocation = StockLocation::findOrFail($request->location_id);
    } catch (\Exception $e){
        if($e instanceof ModelNotFoundException){
            throw new UserNotifyException('This stock location could not be found on the system', 404);
        }
        throw $e;
    }

我的意图是重新抛出任何不是“未找到模型”的异常,并且我希望它的处理方式与try catch不存在的方式完全相同?

我现在意识到我应该写上面的内容如下:

try{
        $stockLocation = StockLocation::findOrFail($request->location_id);
    } catch (ModelNotFoundException $e){
          throw new UserNotifyException('This stock location could not be found on the system', 404);
    }

除此之外,有没有更好的方法来处理我不记录的异常?目前我将异常转换为我自己的自定义异常类(UserNotifyException),然后在Handler.php中我确保没有报告 .