On Thu, Mar 22, 2007 at 02:55:12AM +0900, anselm wrote:
> I have a question : is it possible to catch exceptions that are raised
> on a thread by another thread ? Say if you have two threads, A and B
> and thread A does
> 
> B.raise "bug"
> 
> Is there a way for thread B to catch and process that exception, and
> resume it's execution where it was at  ?

Well, one option is for B to start a new thread C, where it does all its
work. The original thread B hangs around in an infinite loop just catching
and processing exceptions.

require 'thread'
A = Thread.new { sleep 1; B.raise "bug" }
B = Thread.new {
  C = Thread.new {
    loop do
      puts "Ho hum..."
      sleep 3
    end
  }
  begin
    sleep 10000 while true
  rescue Exception => e
    puts "Hey, I got a #{e}"
    retry
  end
}

A.join
B.join
C.join