dqsvnsad79721 2018-10-11 22:08
浏览 497

Laravel - Throw $异常没有被处理程序捕获

In my Laravel 5.6 application, using throw $e seems to output the exception direct to screen and bypasses the Handler.php the exception handler. This means in development the exception is not shown by whoops and in production the exception is not reported.

As I write this, I realise I may have been using try/catches incorrectly but maybe someone can confirm this?

My current code:

    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;
    }

My intention here was to re-throw any exception that is not a "model not found" and I expected it to be handled in exactly the same way as if the try catch was not there at all?

I now realise I should have probably written the above as:

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

Additional to this, is there a better way to handle exceptions that I do not which to be logged? Currently I transform the exception into my own custom exception class (UserNotifyException) and then in Handler.php I ensure this is not reported.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办