Little question about Ruby error handling. I have some code that roughly resembles the following:
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 }
However, when I run this I occasionally come across URLs that redirect, producing the following error:
/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'
with the two last lines referring to the line of code containing the threads.each block.
I was just wondering why I am getting this error considering I have a begin-rescue block in place? Is there something subtle I’m missing here, perhaps to do with the multithreading?
Never mind, silly mistake. I put the begin..rescue block inside the Thread.do block and it worked.