# Catching thread exceptions

**URL:** <https://rubytalk.org/t/catching-thread-exceptions/7302>\
**Category:** ruby-talk\
**Created:** [8 August 2003 17:27 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302 "2003-08-08T17:27:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Tuckner2](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Steve\_Tuckner2](https://rubytalk.org/u/Steve_Tuckner2)\
**Post date:** [8 August 2003 17:27 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302/1 "2003-08-08T17:27:07Z")

</div>

Recently I was helping a colleague who was new to Ruby with a program  
that was using threads. It seemed to him that for some reason that some  
of the thread’s were not running. First he tried adding sleeps to the  
main thread to “give time” to the other threads to run, but this didn’t  
work. It turns out that by joining the threads in question, that there  
were errors in the program that were throwing exceptions that killed the  
thread – SILENTLY.

I don’t know if there is a better way to do this, but this is the  
solution we came up with to print out any exceptions that occur in a  
thread (uncaught) and exit the program.

require “thread”

class MyThread \< Thread  
def initialize(\*args, &block)  
super(\*args) do  
begin  
block.call  
rescue Exception =\> e  
$stderr.print “Exception occured: #{e.message} at " +  
e.backtrace.join(”\n")  
exit! 1  
end  
end  
end  
end

Is there a better way?

Steve Tuckner

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [8 August 2003 17:34 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302/2 "2003-08-08T17:34:48Z")

</div>

> Is there a better way?

Thread.abort\_on\_exception = true

svg% ruby -e 'Thread.new { raise "aa" }; p "after"'  
"after"  
svg%

svg% ruby -e 'Thread.abort\_on\_exception = true; Thread.new { raise "aa" }; p "after"'  
-e:1: aa (RuntimeError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1:in `initialize'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1:in `new'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1  
svg%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [8 August 2003 17:56 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302/3 "2003-08-08T17:56:47Z")

</div>

described at  
[http://www.rubycentral.com/book/tut\_threads.html](http://www.rubycentral.com/book/tut_threads.html)  
and  
[http://www.rubycentral.com/book/ref\_c\_thread.html#Thread.abort\_on\_exception](http://www.rubycentral.com/book/ref_c_thread.html#Thread.abort_on_exception)

Regards,

Brian.

> **···**
>
> On Sat, Aug 09, 2003 at 02:34:48AM +0900, ts wrote:
> 
> > > Is there a better way?
> > 
> > Thread.abort\_on\_exception = true

---

<div class="post-metadata">

**Author:** ![Steve\_Tuckner2](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Steve\_Tuckner2](https://rubytalk.org/u/Steve_Tuckner2)\
**Post date:** [8 August 2003 18:36 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302/4 "2003-08-08T18:36:42Z")

</div>

Why is false by default? In what case would you want an exception to  
propagate to the top of a thread and kill it SILENTLY and then keep running?  
Why doesn’t it at least print something out if it isn’t going to kill the  
program?

We had a problem where a DRb server died silently causing a lot of headache  
trying to figure out why the program was suddenly no longer functioning  
correctly. Any module that a person imports that uses threads (unknown to  
the user of that module) can die and cause problems SILENTLY.

With my change, the exception could always print out a message and die based  
on an option like abort\_on\_exception.

Steve Tuckner

> **···**
>
> > -----Original Message-----  
> > From: ts [[mailto:decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr)]  
> > Sent: Friday, August 08, 2003 12:35 PM  
> > To: ruby-talk ML  
> > Cc: [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> > Subject: Re: Catching thread exceptions
> 
> > > Is there a better way?
> > 
> > Thread.abort\_on\_exception = true
> > 
> > svg% ruby -e ‘Thread.new { raise “aa” }; p “after”’  
> > “after”  
> > svg%
> > 
> > svg% ruby -e ‘Thread.abort\_on\_exception = true; Thread.new {  
> > raise “aa” }; p “after”’  
> > -e:1: aa (RuntimeError)  
> > from -e:1:in `initialize' from -e:1:in `new’  
> > from -e:1  
> > svg%
> > 
> > Guy Decoux

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [8 September 2003 11:58 UTC](https://rubytalk.org/t/catching-thread-exceptions/7302/5 "2003-09-08T11:58:10Z")

</div>

Although a little late I try to answer that one.

“Steve Tuckner” [STUCKNER@MULTITECH.COM](mailto:STUCKNER@MULTITECH.COM) schrieb im Newsbeitrag  
news:00ed01c35ddb$fe568590$6904a8c0@multitech.prv…

> Why is false by default? In what case would you want an exception to  
> propagate to the top of a thread and kill it SILENTLY and then keep  
> running?

IMHO this is the setting that minimally affects stability of a program.  
Imagine a server that spawns threads for requests. If any of those  
threads dies due to some error you don’t want the whole server to stop.  
The proper way to do it is IMHO to handle errors from within the thread:

Thread.new do  
begin  
raise “aa”  
rescue =\> e  
$stderr.puts “ERROR: #{e}”  
else  
$stdout.puts “fine”  
end  
end

> Why doesn’t it at least print something out if it isn’t going to kill  
> the  
> program?

That’s another story and possibly a good suggestion: have ruby print a  
message to STDERR if a thread terminates with an uncaught exception.

Regards

```
robert

```
