我有以下類型的Perl的問題:如何使用Perl異常?
$object1 = $ABC->Find('test1');
然後,我想打電話給一個叫CheckResult
子程序Report.pm
:
$Report->CheckResult($object, "Finding the value");
在另一種情況下,我要報案,如果一個特定的命令是執行的,所以我做這樣的事情:
$Report->CheckResult($ABC->Command(100,100), "Performing the command");
現在Report.pm
:
sub CheckResult {
my ($result, $information) = @_;
# Now, I need something like this
if ($result->isa('MyException')) {
# Some code to create the report
}
}
如何使用異常類以及如何檢查是否引發異常,如果有,請執行必要的任務?
編輯:
截至目前,我有什麼樣的模塊:
package MyExceptions;
use strict;
use warnings;
use Exception::Class (
'MyExceptions',
'MyExceptions::RegionNotFound' => {isa => 'MyExceptions'},
'MyExceptions::CommandNotExecuted' => {isa => 'MyExceptions'}
);
其他模塊:
package ReportGenerator;
use strict;
use warnings;
sub CheckResult {
my ($result, $info) = @_;
# Here is want to check of $result and throw an exception of the kind
# MyExceptions::RegionNotFound->throw(error => 'bad number');
# I'm not sure how to do this
}
1;
用戶將反過來腳本像這樣:
$Report->CheckResult($ABC->Command(100,100), "Tapping Home");
有人可以幫忙嗎?對不起我的無知,我沒有做過任何例外。
我覺得異常::類將滿足我的需要,可能會有人告訴我如何如果你的代碼需要一個模塊來使用它 – MarsMax 2011-04-20 09:34:25