我怎么处理都抓住并未捕获的错误在Perl子程序?
-
22-08-2019 - |
题
这是一个随到 "我怎么可以围绕'死亡'呼吁在Perl图书馆我可不修?".
我有一个子程序,呼吁图书馆-哪个-崩溃的-有时许多倍。而不是沙发上每个电话在这个子程序与eval{},我只是让它死了,和使用eval{}水平上,叫我的子程序:
my $status=eval{function($param);};
unless($status){print $@; next;}; # print error and go to
# next file if function() fails
但是,有误差的条件下,我可以和不赶在功能().什么是最适当的/高雅的方式来设计的错误捕捉中子程序和呼吁程序,所以,我得到正确的行为都抓住并未捕获的错误?
解决方案
块的eval可以嵌套:
sub function {
eval {
die "error that can be handled\n";
1;
} or do {
#propagate the error if it isn't the one we expect
die $@ unless $@ eq "error that can be handled\n";
#handle the error
};
die "uncaught error";
}
eval { function(); 1 } or do {
warn "caught error $@";
};
其他提示
我不能完全确定你想要做什么,但我认为你可以用一个处理程序做到这一点。
$SIG{__DIE__} = sub { print $@ } ;
eval{ function($param); 1 } or next;
不隶属于 StackOverflow