# Handle exception thrown in nested threads from top level thr

**URL:** <https://www.ruby-forum.com/t/handle-exception-thrown-in-nested-threads-from-top-level-thr/69049>\
**Category:** Ruby\
**Created:** [September 18, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/handle-exception-thrown-in-nested-threads-from-top-level-thr/69049 "2006-09-18T12:15:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 18, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/handle-exception-thrown-in-nested-threads-from-top-level-thr/69049/1 "2006-09-18T12:15:25Z")

</div>

In following code, both methos get\_client\_symbol and sync\_clients gets  
called and they start their own new threads. Now, what I would like to  
do is, whenever a exception is raised from the nested threads…i would  
like to catch them in the toplevel thread.

Now…I have tried many things:  
Thread.abort\_on\_exception = true  
and using just rescue or rescue Exception or rescue RuntimeError,  
however the exception raised in nested threads won’t get caught in the  
top level thread. Any clues?

@threads \<\< Thread.new do  
child\_threads = []

```
    begin
       child_threads << get_client_symbol(temp_client_id)
       child_threads << sync_clients(temp_client_id)
      child_threads.each {|thr| p thr.to_s; thr.join; }

     rescue Exception
       p $!
       p $!.backtrace
       # delete the values from list
       @connected_clients.synchronize do
         @connected_clients.delete(temp_client_id)
         @symbols_being_added.signal
       end #end of sync
       child_threads.each {|thr| Thread.kill(thr) if thr.alive? }
     end

  end #end of threads
```

---

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [September 18, 2006, 12:45pm UTC](https://www.ruby-forum.com/t/handle-exception-thrown-in-nested-threads-from-top-level-thr/69049/2 "2006-09-18T12:45:31Z")

</div>

On 18.09.2006 12:15, hemant wrote:

> In following code, both methos get\_client\_symbol and sync\_clients gets  
> called and they start their own new threads. Now, what I would like to  
> do is, whenever a exception is raised from the nested threads…i would  
> like to catch them in the toplevel thread.
> 
> Now…I have tried many things:  
> Thread.abort\_on\_exception = true  
> and using just rescue or rescue Exception or rescue RuntimeError,  
> however the exception raised in nested threads won’t get caught in the  
> top level thread. Any clues?

There is no direct way. If you think about it for a moment it’s not  
possible to catch an exception thrown in another thread. But in Ruby  
you can actually do that by rethrowing it in another thread. Try this:

print "Main thread : ", Thread.current.inspect, “\n”

2.times do  
Thread.new(Thread.current) do |parent|  
print "Child thread: ", Thread.current.inspect, “\n”

```
 loop do
   begin
     puts "."
     raise "Foo" if rand > 0.8
     sleep 1
   rescue Exception => e
     print "Thread : ",
       Thread.current.inspect,
       ": ",
       e.inspect,
       "\n"
     parent.raise e
   end
 end

```

end  
end

10.times do  
begin  
sleep 10  
rescue Exception =\> e  
print "Thread main: ",  
Thread.current.inspect,  
": ",  
e.inspect,  
“\n”  
puts e.backtrace  
end  
end

Kind regards

```
robert
```
