【问题标题】:Laravel: Pass exception to handler from try/catch blockLaravel:将异常从 try/catch 块传递给处理程序
【发布时间】:2018-12-06 13:39:45
【问题描述】:

Laravel 5.7.* 项目。我有一个自定义的App\Exceptions\Handler,我在其中识别了许多不同的异常,并根据环境、异常类型等等做一些事情。

在某些地方我使用try/catch 块,在某些情况下,将整个\Exception $error 传递给Exceptions\Handler 会很方便。

问:如何通过?

【问题讨论】:

    标签: laravel exception-handling


    【解决方案1】:

    你需要在 catch 块中做些什么吗?如果你不这样做,只需删除 try/catch。如果你这样做了,抛出异常。

    try {
    
    ... do something ...
    
    } catch(Exception $e) {
    
    ... do something ...
    
    throw $e;
    
    }
    

    【讨论】:

      【解决方案2】:

      有一个名为 report 的 laravel 辅助函数,它可以让你向 Laravel 错误处理程序发送异常:

      try {
      
        // your code
      
      } catch(Exception $e) {
      
        report($e);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多