# Don't wait for Exception in threads

**URL:** <https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629>\
**Category:** Ruby\
**Created:** [May 4, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629 "2008-05-04T20:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [May 4, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629/1 "2008-05-04T20:47:58Z")

</div>

I have two threads running in parallell, like this:

####################

t1=Thread.new{  
50.times{  
p “thread one”  
sleep 0.1  
}  
}

t2=Thread.new{  
p “thread two”  
raise “error in thread two”  
}

t1.join  
t2.join

#################  
The output is:  
“thread one”  
“thread two”  
“thread one”  
… 48 times more “thread one”  
“thread one”  
testscript.rb:15: error in thread two (RuntimeError)  
from testscript.rb:19:in `join’  
from testscript.rb:19

The program waits for the first thread to complete before telling me  
that the second one raised an exception.  
I don’t want to wait until the first thread finishes to see the  
exception, I want to see it right away.  
How can I do that?

Regards  
Emil

---

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [May 4, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629/2 "2008-05-04T21:03:26Z")

</div>

On May 4, 2008, at 1:48 PM, Emil S. wrote:

> “thread one”  
> I don’t want to wait until the first thread finishes to see the  
> exception, I want to see it right away.  
> How can I do that?

Add this line early in your program:

Thread.abort\_on\_exception = true

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [May 4, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629/3 "2008-05-04T21:07:32Z")

</div>

Hi Emil,

Emil S. schrieb:

> “thread one”  
> I don’t want to wait until the first thread finishes to see the  
> exception, I want to see it right away.  
> How can I do that?

You can do something like:

Thread.abort\_on\_exception = true  
t = []  
t \<\< Thread.new {  
50.times{  
p “thread one”  
sleep 0.1  
}  
}  
t \<\< Thread.new {  
p “thread two”  
raise “error in thread two”  
}  
t.each {|t| t.join }

I dont have tested the example, but this way you should see the  
Exception in the  
Moment it occurs

Andi

---

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [May 4, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/dont-wait-for-exception-in-threads/136629/4 "2008-05-04T21:07:42Z")

</div>

James G. wrote:

> On May 4, 2008, at 1:48 PM, Emil S. wrote:
> 
> > “thread one”  
> > I don’t want to wait until the first thread finishes to see the  
> > exception, I want to see it right away.  
> > How can I do that?
> 
> Add this line early in your program:
> 
> Thread.abort\_on\_exception = true
> 
> Hope that helps.
> 
> James Edward G. II

Thank you, that’s exactly what I am looking for.
