# Threads raising exceptions

**URL:** <https://rubytalk.org/t/threads-raising-exceptions/3680>\
**Category:** ruby-talk\
**Created:** [18 December 2002 08:41 UTC](https://rubytalk.org/t/threads-raising-exceptions/3680 "2002-12-18T08:41:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Bates](https://avatars.discourse-cdn.com/v4/letter/t/3da27b/32.png) [@Tim\_Bates](https://rubytalk.org/u/Tim_Bates)\
**Post date:** [18 December 2002 08:41 UTC](https://rubytalk.org/t/threads-raising-exceptions/3680/1 "2002-12-18T08:41:21Z")

</div>

I have the following piece of threading code:

Thread.new(i) do |j|  
#$SAFE = 4  
b = 0  
loop do  
b += 1  
raise ‘Test’ if j == 3 and b == 100  
sleep(0)  
end  
end

This behaves as expected - if Thread.abort\_on\_exception is true, the program  
throws an exception and dies, otherwise just this thread dies, and no  
exception I can rescue. So far, so good.

If, however, I uncomment the line with $SAFE = 4, it behaves as if  
Thread.abort\_on\_exception is false, and so I can’t catch any exception when a  
thread dies, and so I can’t find out what happened, which I need to! What can  
I do about this?

Tim Bates

> **···**
>
> –  
> [tim@bates.id.au](mailto:tim@bates.id.au)

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [18 December 2002 08:59 UTC](https://rubytalk.org/t/threads-raising-exceptions/3680/2 "2002-12-18T08:59:38Z")

</div>

Hi,

> **···**
>
> In message “Threads raising exceptions” on 02/12/18, Tim Bates [tim@bates.id.au](mailto:tim@bates.id.au) writes:
> 
> > I have the following piece of threading code:
> > 
> > Thread.new(i) do |j|  
> > #$SAFE = 4  
> > b = 0  
> > loop do  
> > b += 1  
> > raise ‘Test’ if j == 3 and b == 100  
> > sleep(0)  
> > end  
> > end
> > 
> > This behaves as expected - if Thread.abort\_on\_exception is true, the program  
> > throws an exception and dies, otherwise just this thread dies, and no  
> > exception I can rescue. So far, so good.
> > 
> > If, however, I uncomment the line with $SAFE = 4, it behaves as if  
> > Thread.abort\_on\_exception is false, and so I can’t catch any exception when a  
> > thread dies, and so I can’t find out what happened, which I need to! What can  
> > I do about this?
> 
> use Thread#join or Thread#value to retrieve exception from the dead  
> thread. It is so to avoid process termination by exceptions from  
> untrusted code. For debugging, $DEBUG=true will print exception names.
> 
> ```
> matz.
> 
> ```
