Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 Net/HTTP中的连接超时错误?_Ruby_Connection - Fatal编程技术网

如何处理ruby Net/HTTP中的连接超时错误?

如何处理ruby Net/HTTP中的连接超时错误?,ruby,connection,Ruby,Connection,我尝试了这段代码,但当代理太慢时,我得到连接超时错误。我怎样才能解决这个问题?我尝试了异常处理,但不起作用。有人能帮忙吗 Net::HTTP.new('example.com', nil, '140.113.182.81', '808').start { |http| begin response = http.request p response rescue Timeout::Error p '

我尝试了这段代码,但当代理太慢时,我得到连接超时错误。我怎样才能解决这个问题?我尝试了异常处理,但不起作用。有人能帮忙吗

 Net::HTTP.new('example.com', nil, '140.113.182.81', '808').start { |http|  

      begin   
        response = http.request  
        p response  
      rescue Timeout::Error  
        p 'timed out'   
      end  
    }  

超时::错误是由
Net::HTTP.connect
方法引起的,该方法由
start
执行,而不是由
request
执行

这意味着为了挽救超时,整个
Net::HTTP
调用应该在begin块内

  begin   
    Net::HTTP.new('example.com', nil, '140.113.182.81', '808').start do |http|  
      response = http.request  
      p response  
    end
  rescue Timeout::Error  
    p 'timed out'   
  end  

这是可行的,但只有rescue异常,rescue Timeout::Error未捕获错误。我不理解注释。您能澄清一下吗?rescue Timeout::Error没有捕获错误,但rescue异常是。begin Net::HTTP.new('example.com',nil,'140.113.182.81','808')。start do | HTTP | response=HTTP.request p response end rescue Timeout::Error p'此处不是'rescue Exception p'此处捕获异常'end