2014-12-19 88 views
0

在Perl我处理错误有:Ruby的错误处理类似于Perl

eval{ 
    }; 
if([email protected]) { 
    } 

在Ruby中我使用:

begin 
rescue Exception => e 
sleep 2 
end 

是用Ruby这样做正确的方式,而将这项工作如果互联网或服务器宕机? 如果上述错误是否有任何方法在Ruby中做类似于Perl?

+0

关于perl'eval {}的附注;'http://stackoverflow.com/a/4006339/223226 – 2014-12-19 11:55:51

回答

2

如果您需要从可能的例外中解救出来,那么您的答案是正确的。您必须:

begin 
    # do some useful but dangerious work 
rescue StandardError => e 
    # something went wrong, try to work around it; 
    # object "e" containts usefull error information 
ensure 
    # anyway, cleanup after doing what you've started 
end 

P.S.如果服务器直接出现故障(例如硬件关闭) - 没有例外的代码处理可以帮助您解决问题。

P.P.S.互联网可能不会很快停止。