关于Ruby错误处理的小问题。我有一些代码,大致类似于以下代码:
urls.each do |url|
begin
threads << Thread.new(url) do |url|
page = open(url)
# some further processing of page
end
rescue
puts "Could not retrieve url"
end
threads.each { |thread| thread.join }但是,当我运行此命令时,我偶尔会遇到重定向的URL,从而产生以下错误:
/usr/lib/ruby/1.8/open-uri.rb:174:in `open_loop': redirection forbidden: // url goes here
from my_file.rb in 'join'
from my_file.rb in 'each'最后两行引用包含threads.each块的代码行。
我只是想知道为什么我会得到这个错误,考虑到我有一个开始救援块在适当的地方?这里是否遗漏了一些微妙的东西,可能与多线程有关?
发布于 2011-10-11 23:55:30
不要紧,愚蠢的错误。我将begin..rescue块放在Thread.do块中,它起作用了。
https://stackoverflow.com/questions/7718165
复制相似问题