我有一个子程序,它调用一个库 – 崩溃 – 有时很多次.而不是使用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 $@";};总结
以上是内存溢出为你收集整理的如何在Perl子例程中处理捕获和未捕获的错误?全部内容,希望文章能够帮你解决如何在Perl子例程中处理捕获和未捕获的错误?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)