# Handling exceptions in Threads

**URL:** <https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126>\
**Category:** Ruby\
**Created:** [July 15, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126 "2008-07-15T14:40:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126/1 "2008-07-15T14:40:31Z")

</div>

Hello guys,

I have made an multithreaded algorithm that triggers around a hundred  
concurrent threads. I have in fact, two versions of the same algorithm,  
one with threads and another with recursive calls.

Each thread execute a query on an Oracle database and according to the  
data, it either calls a new thread and ends, or it stores the result in  
a  
global array. The recursive version follow the same process, but instead  
or running in parallel, it calls the method over and over sequentially.

The recursive version runs perfectly and it ends without problem. The  
multithreaded version fails with an exception that I am not able to  
identify.

If I leave the Thread.abort\_on\_exception = false, the algorithm ends  
after  
triggering about 10 threads. If I set it to Thread.abort\_on\_exception =  
true, the algorithm end abnormally, but without printing any error  
message.

I have added the following code at the end of the method called by the  
thread:

rescue Exception =\> error  
puts “#{error.class}: #{error.message}”

But I cannot get any error message in the console. I am sure that the  
problem is something related with the database, but I do not know how to  
solve the problem if I do not see the error message.

Any ideas?

* * *

Guillermo A.  
Senior Engineer, Koiaka GmbH

Koiaka GmbH  
Riesserkopfstr. 17  
82467 Garmisch-Partenkirchen  
Tel: +49 (0)8821 9679555  
Fax: +49 (0)8821 730 9185  
[Mailto:removed\_email\_address@domain.invalid](Mailto:removed_email_address@domain.invalid)  
[http://www.koiaka.com](http://www.koiaka.com)

Amtsgericht MÃ¼nchen: HR B 161 041  
GeschÃ¤ftsfÃ¼hrer: Guillermo A.  
Sitz: Garmisch-Partenkirchen

Diese Email kann vertrauliche und/oder rechtlich geschÃ¼tzte  
Informationen  
enthalten. Wenn Sie nicht der richtige Adressat sind oder diese Email  
irrtÃ¼mlich erhalten haben, dÃ¼rfen Sie diese weder benutzen, kopieren,  
weiterleiten oder irgend eine MaÃŸnahme einleiten, die im Zusammenhang  
mit  
dem Inhalt dieser Email steht. Informieren Sie bitte sofort den Absender  
und vernichten Sie die irrtÃ¼mlich erhaltene Email vollstÃ¤ndig.  
Vielen Dank!

This e-mail message may contain confidential and/or privileged  
information. If you are not an addressee or otherwise authorized to  
receive this message, you should not use, copy, disclose or take any  
action based on this e-mail or any information contained in the message.  
If you have received this material in error, please advise the sender  
immediately by reply e-mail and delete this message completely.  
Thank you!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126/2 "2008-07-15T17:43:59Z")

</div>

removed\_email\_address@domain.invalid wrote:

> But I cannot get any error message in the console. I am sure that the  
> problem is something related with the database, but I do not know how to  
> solve the problem if I do not see the error message.

Why are you sure it is the database? If your other version (the  
recursive one) works, then that suggests the db calls are ok, doesn’t  
it?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126/3 "2008-07-15T18:25:06Z")

</div>

On Jul 15, 2008, at 6:35 AM, removed\_email\_address@domain.invalid wrote:

> rescue Exception =\> error  
> puts “#{error.class}: #{error.message}”
> 
> But I cannot get any error message in the console. I am sure that the  
> problem is something related with the database, but I do not know  
> how to  
> solve the problem if I do not see the error message.
> 
> Any ideas?

1. STDERR.puts message

stdout bufferes and can get lost

1. it’s _highly_ unlikely the db handle is thread safe and, if it is,  
it’ll be native thread safe not green thread safe. aka - you will not  
get anything from using MT to execute your queries.

so, just use producer/consumer to both simplify and speed up your  
application. for example

q = Queue.new

queries = list\_of\_queries

done = Object.new.freeze

producer = Thread.new do  
Thread.current.abort\_on\_exception

queries.each do |query|  
q.push db.execute(query)  
end

q.push done  
end

while( ( result = q.pop) != done )  
handle\_result result  
end

you could handle the results in threads too using the same pattern.  
the key is here is simply to eliminate the MT aspect of the db handle  
usage - for sanity and correctness.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 16, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/handling-exceptions-in-threads/143126/4 "2008-07-16T12:01:40Z")

</div>

Hello,

I have solved the problem. It was something like this:

def mymethod  
row = myselect.execute(select)  
if row.next  
Thread.new {mymethod(row.getcursorvalue)}  
end  
Rescue  
puts error  
end

As you can see the the variable “row” is local to the method, but when I  
was including it inside of the thread block, it becomes local of the  
thread. The database was answering with an error like this “No cursor  
opened”, since the cursor is not valid in the thread context. The Rescue  
clause was not responding, since the error was introduced before the  
method was actually called.

> you could handle the results in threads too using the same pattern.  
> the key is here is simply to eliminate the MT aspect of the db handle  
> usage - for sanity and correctness.

I am using JRuby 1.1.2 with the Oracle JDBC. The engine is an Oracle 11i  
running on a separated server, but sometimes I use it in my own MacBook  
PRO on a linux vmware virtual machine. Do you think it is not thread  
safe?  
also, why do you think that it is not good to run the queries in  
parallel?

Cheers,

* * *

Guillermo A.  
Senior Engineer, Koiaka GmbH

Koiaka GmbH  
Riesserkopfstr. 17  
82467 Garmisch-Partenkirchen  
Tel: +49 (0)8821 9679555  
Fax: +49 (0)8821 730 9185  
[Mailto:removed\_email\_address@domain.invalid](Mailto:removed_email_address@domain.invalid)  
[http://www.koiaka.com](http://www.koiaka.com)

Amtsgericht MÃ¼nchen: HR B 161 041  
GeschÃ¤ftsfÃ¼hrer: Guillermo A.  
Sitz: Garmisch-Partenkirchen

Diese Email kann vertrauliche und/oder rechtlich geschÃ¼tzte  
Informationen  
enthalten. Wenn Sie nicht der richtige Adressat sind oder diese Email  
irrtÃ¼mlich erhalten haben, dÃ¼rfen Sie diese weder benutzen, kopieren,  
weiterleiten oder irgend eine MaÃŸnahme einleiten, die im Zusammenhang  
mit  
dem Inhalt dieser Email steht. Informieren Sie bitte sofort den Absender  
und vernichten Sie die irrtÃ¼mlich erhaltene Email vollstÃ¤ndig.  
Vielen Dank!

This e-mail message may contain confidential and/or privileged  
information. If you are not an addressee or otherwise authorized to  
receive this message, you should not use, copy, disclose or take any  
action based on this e-mail or any information contained in the message.  
If you have received this material in error, please advise the sender  
immediately by reply e-mail and delete this message completely.  
Thank you!

From:  
“ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To:  
removed\_email\_address@domain.invalid (ruby-talk ML)  
Date:  
15.07.2008 18:24  
Subject:  
Re: Handling exceptions in Threads

On Jul 15, 2008, at 6:35 AM, removed\_email\_address@domain.invalid wrote:

> rescue Exception =\> error  
> puts “#{error.class}: #{error.message}”
> 
> But I cannot get any error message in the console. I am sure that the  
> problem is something related with the database, but I do not know  
> how to  
> solve the problem if I do not see the error message.
> 
> Any ideas?

1. STDERR.puts message

stdout bufferes and can get lost

1. it’s _highly_ unlikely the db handle is thread safe and, if it is,  
it’ll be native thread safe not green thread safe. aka - you will not  
get anything from using MT to execute your queries.

so, just use producer/consumer to both simplify and speed up your  
application. for example

q = Queue.new

queries = list\_of\_queries

done = Object.new.freeze

producer = Thread.new do  
Thread.current.abort\_on\_exception

queries.each do |query|  
q.push db.execute(query)  
end

q.push done  
end

while( ( result = q.pop) != done )  
handle\_result result  
end

you could handle the results in threads too using the same pattern.  
the key is here is simply to eliminate the MT aspect of the db handle  
usage - for sanity and correctness.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
