From: Robert Klemme Date: 2004-12-20T18:22:06+09:00 Subject: Re: threads and errors "Charles Mills" schrieb im Newsbeitrag news:1103500878.714949.188970@c13g2000cwb.googlegroups.com... > What techniques are available for handling errors that occur in > threads? > > For example how do I find out that an error occured in t below? > > t = Thread.new { raise "error" } > > I need to be able to check on the error status of threads from their > parent. Something like the following would be ideal - but I know this > won't do what I want: > > begin > Thread.new { raise "error"} > rescue > puts $! # in a strange world prints "error", but in actuality does > nothing > end Apart from the other proposed solutions you can catch the exception at the main level of the thread (Test 0) or you can use "Thread.abort_on_exception = true" (Test 1 and 2): 10:20:08 [ruby]: ruby thread-abort-test.rb # thread-abort-test.rb:2 thread-abort-test.rb:4 thread-abort-test.rb:6 thread-abort-test.rb:7: Test 2 (RuntimeError) from thread-abort-test.rb:7:in `initialize' from thread-abort-test.rb:7:in `new' from thread-abort-test.rb:7 10:20:19 [ruby]: cat thread-abort-test.rb Thread.new { begin; raise "Test 0"; rescue Exception => e; p e; end } puts "#{__FILE__}:#{__LINE__}" Thread.new { raise "Test 1" } puts "#{__FILE__}:#{__LINE__}" Thread.abort_on_exception = true puts "#{__FILE__}:#{__LINE__}" Thread.new { raise "Test 2" } puts "#{__FILE__}:#{__LINE__}" Kind regards robert