即使我已将 DBI 调用包装在 eval 中,为什么我仍会在控制台上看到 DBI 错误?
我有一个在 eval 中运行的数据库查询,以捕获错误。问题是错误消息正在输出到控制台,即使它被捕获。当我想自己解析它并吐出我自己的消息时,如何阻止错误消息这样做?
my $dbh = DBI->connect('dbi:Pg:dbname=database;host=localhost',
'user', 'pass',
{RaiseError => 1}
);
eval{
$sth = $dbh->prepare($sql);
$sth->execute;
};
if($@){
#Do my parse/print stuff here I know
}
I have a database query that I am running inside an eval, to trap the error. Problem is that the error message is outputting to console, even though it is being trapped. How do I stop the error message from doing this, as I want to parse it myself and spit back my own messages?
my $dbh = DBI->connect('dbi:Pg:dbname=database;host=localhost',
'user', 'pass',
{RaiseError => 1}
);
eval{
$sth = $dbh->prepare($sql);
$sth->execute;
};
if($@){
#Do my parse/print stuff here I know
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获和忽略错误都不是一个好主意,无论它们是否致命。另外,不建议以您正在执行的方式检查 $@ (请参阅本网站上有关 perl 异常的问题,以获取捕获异常的更好方法;我使用 Try::Tiny 下面,这可以说是最轻量级的路线)。
当先前的操作可能失败时,您不应继续执行 DBI 操作,而应在每一步检查错误条件:
并记住,始终
使用严格;在每个模块和脚本中使用警告;
。您的代码摘录表明您尚未执行此操作。It's not a good idea to trap and ignore errors, whether they are fatal or not. Also, it is not advisable to check $@ in the way you are doing it (see the questions on this site about perl exceptions for better ways to trap exceptions; I use Try::Tiny below, which is arguably the lightest-weight route of all).
Instead of proceeding with a DBI operation when an earlier one might have failed, you should check error conditions at every step:
And remember, always
use strict; use warnings;
in every module and script. Your code excerpt suggests that you are not yet doing this.您可以指定 'PrintError => 0' 在您的
connect
调用中(或使用 HandleError ):或者设置每个语句句柄:
另外,不要依赖 $@ 来指示错误。使用 eval 的更好方法是:
You can specify 'PrintError => 0' in your
connect
call (or use HandleError):Or to set per statement handle:
Also, don't depend on $@ for indicating an error. A better way to use eval is:
eval { }
将捕获致命错误(来自die
或Carp::croak
调用),但不会捕获非致命错误消息(来自warn
或carp
)。要处理警告消息,请参阅%SIG< 的文档中如何安装警告处理程序/code>
或
警告
。一个简单的解决方法是在您的
eval
块中使用一个简单的警告处理程序。另请参阅:
perlfaq7:
如何暂时阻止警告?eval { }
will trap a fatal error (from adie
orCarp::croak
call), but not a non-fatal error message (fromwarn
orcarp
). To handle warning messages, see how to install a warning handler in documentation for%SIG
orwarn
.A trivial workaround is to use a trivial warning handler inside your
eval
block.See also:
perlfaq7:
How do I temporarily block warnings?