【问题标题】:laravel unable to handle exception, catch block not executinglaravel 无法处理异常,catch 块未执行
【发布时间】:2018-09-03 06:22:15
【问题描述】:

我正在创建一个在远程服务器上执行各种操作的 SSH Bot。这些服务器的用户名和密码由用户提供。

但是当用户提供错误的用户名或密码或错误的服务器IP地址时,它会抛出ErrorException异常。

我想处理该异常并将错误消息存储在数据库中。而不是向用户展示它。

这是我的代码

try {
                $pending_server->console_output = $this->setupDNS($pending_server->toArray());
                $pending_server->status = 'Configured';
            } catch (ErrorException $e) {
                $pending_server->console_output = $e->getMessage;
                $pending_server->status = 'Failed';
                $pending_server->save();
            }

数据库中$pending_server是数据库模型。 setupDNS 方法正在引发异常。 因此,如果出现异常,我想将错误消息作为输出存储在数据库中。但是 catch 块根本没有执行,脚本的执行停止了。

【问题讨论】:

  • 看看日志说什么会很有趣。您可以在启动 SSH 连接之前通过 define('NET_SSH2_LOGGING', 3); 获取它们。
  • @neubert 我知道异常的原因,所以看到日志后我该怎么办
  • 我只想知道为什么catch块没有被执行

标签: php exception-handling laravel-5.6


【解决方案1】:

只需将“\”放在 ErrorException 之前。例如:

try {
                $pending_server->console_output = $this->setupDNS($pending_server->toArray());
                $pending_server->status = 'Configured';
            } catch (\ErrorException $e) {
                $pending_server->console_output = $e->getMessage;
                $pending_server->status = 'Failed';
                $pending_server->save();
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多