rescue the exception at the time the threads are joined. : join « Threads « Ruby






rescue the exception at the time the threads are joined.


threads = [] 

4.times do |number| 
    threads << Thread.new(number) do |i| 
        raise "Boom!" if i == 2 
        print "#{i}\n" 
    end 
end 

threads.each do |t| 
    begin 
        t.join 
    rescue RuntimeError => e 
        puts "Failed: #{e.message}" 
    end 
end 

 








Related examples in the same category

1.each thread is given only one second to execute:
2.Join threads
3.Multithreaded Applications in Ruby
4.Join two threads
5.Join a thread after sleeping