如何在Perl子例程中处理捕获和未捕获的错误?
发布时间:2020-12-16 06:28:34 所属栏目:大数据 来源:网络整理
导读:这是 “How can I get around a ‘die’ call in a Perl library I can’t modify?”的后续行动. 我有一个子程序,它调用一个库 – 崩溃 – 有时很多次.而不是使用eval {}在这个子例程中调用每个调用,我只是让它死掉,并在调用我的子例程的级别上使用eval {}:
这是
“How can I get around a ‘die’ call in a Perl library I can’t modify?”的后续行动.
我有一个子程序,它调用一个库 – 崩溃 – 有时很多次.而不是使用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 handledn"; 1; } or do { #propagate the error if it isn't the one we expect die $@ unless $@ eq "error that can be handledn"; #handle the error }; die "uncaught error"; } eval { function(); 1 } or do { warn "caught error $@"; }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |