我是否需要在对 Win32::OLE->LastError 的调用中捕获错误?

发布于 2024-08-07 21:17:27 字数 950 浏览 11 评论 0原文

[编辑] - 事后看来,这个问题被误导了。我没有删除它,因为它是不正确使用evalPerl::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-08-14 21:17:27

撇开 perl-critic 问题不谈,您的代码没有多大意义。

Win32::OLE 文档 解释何时抛出异常(以及如何自动捕获它们)。

LastError 只是在错误发生后为您提供有关错误的信息(假设您的程序尚未终止)。将其包装在 eval 中是没有意义的。

更新:我会写一些类似以下内容的内容(未经测试,因为我现在使用的是 Linux,无法访问 Windows):

use strict;
use warnings;

use Carp;

use Win32;
use Win32::OLE;

$Win32::OLE::Warn = 3;

# No need for this eval if you are OK with the default error message    
my $Jet = eval {
    Win32::OLE->CreateObject('DAO.DBEngine.36')
} or croak sprintf(
    "Can't create Jet database engine: %s", 
    win32_error_message(Win32::OLE->LastError)
);

# No need for this eval if you are OK with the default error message
my $DB = eval {
    $Jet->OpenDatabase($DBFile)
} or croak sprintf(
    "Can't open database '$DBFile': %s",
    win32_error_message(Win32::OLE->LastError)
);

my $result = eval {
    $DB->Execute( $SQLquery, 128 )
};

unless (defined $result) {
    print $ERROR win32_error_message(Win32::OLE->LastError);
    Win32::OLE->LastError(0);
}

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 in eval 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):

use strict;
use warnings;

use Carp;

use Win32;
use Win32::OLE;

$Win32::OLE::Warn = 3;

# No need for this eval if you are OK with the default error message    
my $Jet = eval {
    Win32::OLE->CreateObject('DAO.DBEngine.36')
} or croak sprintf(
    "Can't create Jet database engine: %s", 
    win32_error_message(Win32::OLE->LastError)
);

# No need for this eval if you are OK with the default error message
my $DB = eval {
    $Jet->OpenDatabase($DBFile)
} or croak sprintf(
    "Can't open database '$DBFile': %s",
    win32_error_message(Win32::OLE->LastError)
);

my $result = eval {
    $DB->Execute( $SQLquery, 128 )
};

unless (defined $result) {
    print $ERROR win32_error_message(Win32::OLE->LastError);
    Win32::OLE->LastError(0);
}
婴鹅 2024-08-14 21:17:27

该消息的含义在 文档。简而言之,它告诉您在 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 an eval, but to also check the return value of eval.

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 of eval is completely superfluous because the method you are calling shouldn't be throwing any exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文