如何处理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