# Exception never raised when threading an eval containing a m

**URL:** <https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498>\
**Category:** Ruby\
**Created:** [February 5, 2007, 11:05am UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498 "2007-02-05T11:05:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkswoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@darkswoop](https://www.ruby-forum.com/u/darkswoop)\
**Post date:** [February 5, 2007, 11:05am UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498/1 "2007-02-05T11:05:09Z")

</div>

Hello everyone!

I’ve encountered a strange issue when coding a tool extending ruby, and  
was wondering if I was missing something… I’m working under windows  
with  
the last stable release of ruby (1.8.5-21) and did not find a way to  
debug  
ruby sources to observe the behaviour. This is reproductible with just  
the  
common ruby interpreter. Let me explain the problem :

First let’s consider this small script :

#!/usr/bin/env ruby  
$FOO+2

We get the evident result ($FOO is not defined, an exception’s raised) :

test.rb:2: undefined method `+’ for nil:NilClass (NoMethodError)

Then let’s consider this small script :

#!/usr/bin/env ruby  
eval("puts “foo”)

You can see that the string given to puts is malformed, so we get again  
an  
exception :

(eval):1: compile error (SyntaxError)  
(eval):1: unterminated string meets end of file

Ok. Now let’s thread this. Let’s consider the following script :

#!/usr/bin/env ruby  
def newthread()  
toret = Thread.new {  
puts “START”  
begin  
$FOO+2  
rescue  
puts(“EXCEPTION !!!”)  
end  
puts “END”  
}  
return toret  
end

$TESTTHREAD = newthread()  
2.times {  
if(!$TESTTHREAD.alive?) then  
puts “DEAD, RESURRECTING”  
$TESTTHREAD = newthread()  
sleep(4)  
end  
}

We create a thread, provoke an exception, catch it, restart the thread,  
and so on twice again. We get the following expected behaviour :

START  
EXCEPTION !!!  
END  
DEAD, RESURRECTING  
START  
EXCEPTION !!!  
END  
DEAD, RESURRECTING  
START  
EXCEPTION !!!  
END

But now, let’s consider this (here comes the issue) : we do exactly the  
same thing (we only replace the $FOO+2 line by our malformed eval-puts)  
:

#!/usr/bin/env ruby  
def newthread()  
toret = Thread.new {  
puts “START”  
begin  
eval("puts “foo”)  
rescue  
puts(“EXCEPTION !!!”)  
end  
puts “END”  
}  
return toret  
end

$TESTTHREAD = newthread()  
2.times {  
if(!$TESTTHREAD.alive?) then  
puts “DEAD, RESURRECTING”  
$TESTTHREAD = newthread()  
sleep(4)  
end  
}

And then we get…

START  
DEAD, RESURRECTING  
START  
DEAD, RESURRECTING  
START

(!!) It simply seems that the ruby thread dies inside the eval, and the  
exception is never raised (?). Has someone an explanation for this ?  
(found a bug somewhere ?) 🙂

Many thanks by advance for those who’d work on that matter!

Ben

---

<div class="post-metadata">

**Author:** ![darkswoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@darkswoop](https://www.ruby-forum.com/u/darkswoop)\
**Post date:** [February 5, 2007, 11:20am UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498/2 "2007-02-05T11:20:14Z")

</div>

On 2/5/07, Benjamin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> eval("puts \"foo")
> def newthread()
> end
> 
> ```
> 
> We create a thread, provoke an exception, catch it, restart the thread,  
> START  
> begin  
> 2.times {  
> DEAD, RESURRECTING  
> START  
> DEAD, RESURRECTING  
> START
> 
> (!!) It simply seems that the ruby thread dies inside the eval, and the  
> exception is never raised (?). Has someone an explanation for this ?  
> (found a bug somewhere ?) 🙂
> 
> Many thanks by advance for those who’d work on that matter!

Hi,

it’s easy:  
$FOO+2 raises NoMethodError \< StandardError  
while eval(…) raises SyntaxError !\< StandardError

empty rescue == rescue StandardError

If you want to catch ALL exceptions, use rescue Exception.

For exceptions hierarchy see

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

---

<div class="post-metadata">

**Author:** ![darkswoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@darkswoop](https://www.ruby-forum.com/u/darkswoop)\
**Post date:** [February 5, 2007, 11:27am UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498/3 "2007-02-05T11:27:15Z")

</div>

> For exceptions hierarchy see  
> [Ruby | zenspider.com | by ryan davis](http://www.zenspider.com/Languages/Ruby/QuickRef.html#34)

Thanks Jan ! Really easy indeed, and it solved my problem :).

Ben

---

<div class="post-metadata">

**Author:** ![darkswoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@darkswoop](https://www.ruby-forum.com/u/darkswoop)\
**Post date:** [June 17, 2011, 1:45pm UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498/4 "2011-06-17T13:45:41Z")

</div>

i need datasheet (schematic) chipset coolsand CT1128, anyone please help  
me…

---

<div class="post-metadata">

**Author:** ![darkswoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@darkswoop](https://www.ruby-forum.com/u/darkswoop)\
**Post date:** [February 5, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/exception-never-raised-when-threading-an-eval-containing-a-m/84498/5 "2007-02-05T22:03:15Z")

</div>

Hi~

On Feb 5, 2007, at 2:04 AM, Benjamin B. wrote:

> First let’s consider this small script :
> 
> ```
> }
> 
> ```
> 
> }  
> EXCEPTION !!!  
> #!/usr/bin/env ruby  
> return toret
> 
> this ? (found a bug somewhere ?) 🙂
> 
> Many thanks by advance for those who’d work on that matter!
> 
> Ben

Ben-

By default ruby doesn’t propogate exceptions up out of the threads,  
if an exception is raised in a thread that thread will just silently  
dies. To make exceptions in a thread raise to the top level you need  
to set this in your script:

Thread.abort\_on\_exception = true

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
