I think the line “puts “hello”” should not be run after raising an
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
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)