【问题标题】:Laravel custom Exception handler not runningLaravel 自定义异常处理程序未运行
【发布时间】:2014-05-06 14:10:38
【问题描述】:

我关注tutorial 并查看Laravel's docs 以注册自定义错误处理程序。

我注册了这个类,然后抛出 MyCustomException,但由于某种原因,它忽略了其中的所有内容,只运行常规的 Exception 类。下面的代码打印出exception 'MyCustomException' with message 'This is NOT the message I want to see' 而不是“这是自定义异常消息”

目前下面的所有代码都只是在一个测试页面上,但我已经尝试在 Exception 之前将类(并将 MyCustomException 声明)注册到 global.php 中,并且我也在 E​​xception 之后尝试过。没有任何变化。

我也在 MyCustomException 中尝试过 sleep(10),但没有运行; MyCustomException 只是没有运行。

我做错了什么?

编辑:事实上,从教程中复制和粘贴代码的结果与我的自定义代码相同;自定义异常处理程序没有运行。

class MyCustomException extends Exception {}

App::error(function(MyCustomException $exception) { 
    return "This is the custom exception message.";
});

//Now throw the error and see what comes out
try {
    throw new MyCustomException('This is NOT the message I want to see');
} catch (MyCustomException $e) {
    die($e);
}

【问题讨论】:

    标签: php exception-handling error-handling laravel laravel-4


    【解决方案1】:

    你现在可能已经解决了这个问题,但你想要的是$e->getMessage()

    使用 PHP 5.1+ 将打印您的异常消息。

    文档:http://php.net/manual/en/exception.getmessage.php

    【讨论】:

      【解决方案2】:

      请这样尝试

      throw new MyCustomException('This is NOT the message I want to see');
      

      【讨论】:

      • 因此,如果没有try catch{},它只会将相同的内容打印到错误日志而不是屏幕上。 [2014-03-27 14:26:23] local.ERROR: exception 'MyCustomException' with message 'This is NOT the message I want to see'
      • 应该是打印This is the custom exception message.
      • 我试过了,它打印了,“这是自定义异常消息”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2023-03-07
      • 2011-07-13
      • 2015-10-18
      • 2023-03-04
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多