From: Oliver Peng Date: 2007-06-08T03:53:54+09:00 Subject: Re: raise problem in Thread Hi Joel: Thank you for your reply. One thing I am a little confused is that the main thread continues executing the puts "hello". From my point of view, puts "hello" is part of method test_raise. So after the exception was raised and the MyThread instance was dead, the method should stopping running and just return nil to main thread. Why main thread need to continue running left part of this method? This can cause some unexpecting problems. Thanks. Joel VanderWerf wrote: > oliver wrote: >> raise StandardError.new() >> >> 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) -- Posted via http://www.ruby-forum.com/.