php - laravel 无法处理异常,catch 块未执行
问题描述
我正在创建一个在远程服务器上执行各种操作的 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 块根本没有执行,并且脚本的执行停止了。
解决方案
只需将“\”放在 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();
}
推荐阅读
- python - 用矢量化/应用替换 pandas iterrows
- regex - 如何从 HTML 中提取名称和标题?
- scala - 在火花中使用 withColumn 将列转换为小写不起作用
- java - 记录所有 Thrift 服务器的请求和响应
- azure-devops - 带有 gradle 版本的 Azure CI 管道 - 跳过任务
- java - 如何正确从 url 读取 JSON 数组数据
- r - ggplot2:将标签添加到百分比图(不是位置=填充,而只是填充)
- python-3.x - 如何将循环生成的 tkinter 输入框的输入返回到列表以供以后使用
- mysql - Mysql Join 两个表并使用 If 条件获取结果
- html - 如何在 HTML5 仪表中创建刻度?