From: Joel VanderWerf Date: 2007-06-08T03:23:03+09:00 Subject: Re: raise problem in Thread oliver wrote: > Define following class: > class MyThread < Thread > def initialize() > super{ > puts "started" > sleep 1000 > } > end > > def test_raise() > raise StandardError.new() > puts "hello" > end > end > > Open irb and run: > irb(main):082:0> thread = MyThread.new > started=> # > irb(main):093:0> thread.test_raise > hello > => nil > > I think the line "puts "hello"" should not be run after raising an > exception. > What is the reason of this problem? The problem is that test_raise is executed not by the MyThread thread, but by the main thread. The MyThread instance receives the exception and dies (silently because abort_on_exception is off), and the main thread continues executing the puts "hello". Keep in mind that the following contexts are different: (1) *self*, the object that receives a message and performs the lookup to determine which method handles the message (2) Thread.current: the thread that the interpreter is currently running to execute a method Let's look at the context when you call test_raise. Even though the *self* is your MyThread instance, the current thread is still the main thread. If you want to send commands to a thread and have them executed _by_ the thread, you might want to set up a Queue that it reads. Also, it's useful to have the following when debugging threads: Thread.abort_on_exception = true (do this at the beginning of your program--you can also set this for individual threads, but the global setting is nice for debugging) -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407