Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby中的异常处理-调用开始(如果它转到rescue)_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Ruby中的异常处理-调用开始(如果它转到rescue)

Ruby on rails Ruby中的异常处理-调用开始(如果它转到rescue),ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个简单的异常处理块 begin <connect to network and make a request> rescue <comes here if it didnt connect / whatever other error> end 开始 营救 结束 我想修改它,这样,如果它来救援-它会再次开始。试一试。如果仍然未连接-从块中出来增加一个计数器,然后重试,直到该计数器达到以下值: 最大重试次数=5次 重试次数=0 开始 做点什么 营救

我有一个简单的异常处理块

begin
    <connect to network and make a request>
rescue
    <comes here if it didnt connect / whatever other error>
end
开始
营救
结束

我想修改它,这样,如果它来救援-它会再次开始。试一试。如果仍然未连接-从块中出来

增加一个计数器,然后重试,直到该计数器达到以下值:

最大重试次数=5次
重试次数=0
开始
做点什么
营救前
如果重试次数+=1则应执行以下操作:

 5.times do |i|
   begin
     1/0
   rescue
     puts 'Try #{i+1} failed ...'
     next
   end
   puts 'Try #{i+1} success.'
   break
 end

您需要关键字
重试
。再加上最大重试次数。i、 e.
if(最大重试次数-=1)>0

重试

编写一个连接到网络的方法,如下所示

def connect_to_server(retry_count)
  begin
   retry_count += 1
   <connect to network and make a request>
   rescue
     if(retry_count <= 5)
       connect_to_server(retry_count)
     end
  end
end

那么为什么要使用begin resue block?不情愿的+1:
MAX\u RETRIES-=1
是错误的(修改常量)@JulienPortalier说得对,说得对。我只是用它来说明这个想法,但你是对的。
connect_to_server(0)