【问题标题】:Laravel exception not catchingLaravel 异常没有捕获
【发布时间】:2017-11-17 15:27:56
【问题描述】:

我正在尝试做一个非常基本的异常尝试捕获,但它没有捕获。

 $id =0;
 try {
        $question = $this->model->find($id);  // will not find anything since $id = 0
        $question->delete(); // throw an exception
        return true;
 } catch (\Exception $e) {
        dd ('hello');  // should end up here, but no?!?!? 
 } catch (FatalThrowableError $f) {
        echo ("fatal"); // or here... but no. 
 }

但捕获不会“捕获”。我在浏览器中收到一个致命错误,说删除是在一个空对象上调用的。但这正是我想要做的:对空对象执行删除(id = 0 不在数据库中),以测试异常。

我试过了

use Symfony\Component\Debug\Exception;
use Symfony\Component\Debug\Exception\FatalThrowableError;

或者干脆

Exception;
FatalThrowableError;

此外,拥有 \Exception $e 或 Exception $e (带或不带 )不会改变任何东西。

请注意,如果我添加像 $foo = 4/0 这样的行,我会进入异常部分 (dd (hello))。

在.env APP_DEBUG=true, APP_LOG_LEVEL=debug

我在 Laravel 5.5 上使用 Windows 7 上的 PHP 7.0.10。

【问题讨论】:

标签: php laravel


【解决方案1】:

http://php.net/manual/en/language.errors.php7.php

由于Error 层次结构不继承自Exception,因此使用的代码 catch (Exception $e) { ... } 块来处理未捕获的异常 PHP 5 会发现这些Errors 没有被这些块捕获。 catch (Error $e) { ... } 块或 set_exception_handler() 处理程序是必需的。

此外,您还可以使用catch (\Throwable $e) {} 来解释ErrorException 类型。

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2015-09-21
    • 1970-01-01
    • 2021-06-20
    • 2013-03-24
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多