我是否需要在对 Win32::OLE->LastError 的调用中捕获错误?
[编辑] - 事后看来,这个问题被误导了。我没有删除它,因为它是不正确使用eval和Perl::Critic正确批评的一个很好的例子。
Perl Critic 对以下代码提出了以下批评:
eval 的返回值未测试。您不能依赖
$@/$EVAL_ERROR
的值来判断 eval 是否失败
my $Jet = Win32::OLE->CreateObject('DAO.DBEngine.36')
or croak "Can't create Jet database engine.";
my $DB = $Jet->OpenDatabase($DBFile)
# code omitted for the sake of brevity
# perl script writes results to Access db via an append query
$DB->Execute( $SQLquery, 128 ); #128=DBFailOnError
eval {$err = Win32::OLE->LastError()} ; #<<<< PROBLEM LINE SEE FEEDBACK BELOW
if ( $err){
print $ERROR "WIN32::OLE raised an exception: $err\n";
Win32::OLE->LastError(0); # this clears your error
}
我的想法是我正在使用 eval
来检测错误对象的存在并在 Win32:OLE
模块上检测错误并报告它。
我可以安全地忽略这些批评吗?
[EDIT] - with the benefit of hindsight, this question was misdirected. I have not deleted it because it is a good example of the incorrect use of eval and correct criticism by Perl::Critic.
Perl Critic raises the following criticism for the code below:
Return value of eval not tested. You can't depend upon the value of
$@/$EVAL_ERROR
to tell whether an eval failed
my $Jet = Win32::OLE->CreateObject('DAO.DBEngine.36')
or croak "Can't create Jet database engine.";
my $DB = $Jet->OpenDatabase($DBFile)
# code omitted for the sake of brevity
# perl script writes results to Access db via an append query
$DB->Execute( $SQLquery, 128 ); #128=DBFailOnError
eval {$err = Win32::OLE->LastError()} ; #<<<< PROBLEM LINE SEE FEEDBACK BELOW
if ( $err){
print $ERROR "WIN32::OLE raised an exception: $err\n";
Win32::OLE->LastError(0); # this clears your error
}
My thinking is that I am using eval
to detect the existence of the error object and on the Win32:OLE
module to detects the error and reports it.
Am I safe to ignore the criticism?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
撇开
perl-critic
问题不谈,您的代码没有多大意义。Win32::OLE 文档 解释何时抛出异常(以及如何自动捕获它们)。
LastError
只是在错误发生后为您提供有关错误的信息(假设您的程序尚未终止)。将其包装在eval
中是没有意义的。更新:我会写一些类似以下内容的内容(未经测试,因为我现在使用的是 Linux,无法访问 Windows):
Leaving aside the
perl-critic
issuse, your code does not make much sense.The Win32::OLE docs explain when exceptions will be thrown (and how you can automatically catch them).
LastError
just gives you information about an error after it has occurred assuming your program has not died. Wrapping it ineval
is pointless.Update: I would have written something along the following lines (untested because I am on Linux with no access to Windows right now):
该消息的含义在 文档。简而言之,它告诉您在
eval
之后不要单独依赖$@
,还要检查eval
的返回值。但是,就您而言,问题是您既没有检查
eval
的返回值,也没有检查$@
而且似乎您使用了 < code>eval 完全是多余的,因为您调用的方法不应该抛出任何异常。The meaning of that message is detailed in the documentation. In short, it tells you to not rely on
$@
alone after aneval
, but to also check the return value ofeval
.However, in your case, the problem is that you aren't checking either the return value of
eval
nor are you checking$@
and moreover it seems that your use ofeval
is completely superfluous because the method you are calling shouldn't be throwing any exceptions.