【问题标题】:Ruby error handling similar to Perl类似于 Perl 的 Ruby 错误处理
【发布时间】:2023-03-28 09:28:01
【问题描述】:

在 Perl 中,我处理错误:

eval{
     };
if($@) {
     }

我在 Ruby 中使用过:

 begin
 rescue Exception => e
 sleep 2
 end 

这是在 Ruby 中正确的做法吗?如果 Internet 或服务器出现故障,这是否可行? 如果上述错误,有什么方法可以在 Ruby 中类似于 Perl 吗?

【问题讨论】:

标签: ruby perl error-handling


【解决方案1】:

如果您需要从可能的异常中解救出来,那么您做对了。你必须:

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

附:如果服务器真的宕机了(例如硬件关闭)——没有异常代码处理可以帮助你。

附言互联网可能不会很快关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多