# Trapping error not caught by rescue Exception -- How to?

**URL:** <https://rubytalk.org/t/trapping-error-not-caught-by-rescue-exception-how-to/33797>\
**Category:** ruby-talk\
**Created:** [17 December 2006 02:50 UTC](https://rubytalk.org/t/trapping-error-not-caught-by-rescue-exception-how-to/33797 "2006-12-17T02:50:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [17 December 2006 02:50 UTC](https://rubytalk.org/t/trapping-error-not-caught-by-rescue-exception-how-to/33797/1 "2006-12-17T02:50:07Z")

</div>

Hi,

The fourth data line in the program below hangs rather than submitting  
to the rescue clause. How can I trap that kind of error?

I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, MySQL  
5.0.15-nt, SciTE 1.59

TIA,  
Richard

=== code start ===  
begin  
&nbsp;&nbsp;DATA.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;var, expr, excess = line.chomp.split(/=/, 3)  
&nbsp;&nbsp;&nbsp;&nbsp;var.strip!; expr.strip!  
&nbsp;&nbsp;&nbsp;&nbsp;puts "expr=" + expr + ", expr.class=" +expr.class.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;puts "eval(expr)=" + eval(expr).to\_s  
&nbsp;&nbsp;end  
rescue Exception =\> eMsg  
&nbsp;&nbsp;puts "ERROR: " + eMsg  
&nbsp;&nbsp;retry  
end

\_\_END\_\_  
a = 5\*\*2 =# 25  
b =date =# undefined local variable or method `date' for  
main:Object  
c = y =# undefined local variable or method `y' for main:Object  
c =`date` =# (ruby hangs; Ctrl-C kills it)  
=== code end ===

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie1](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@Daniel\_Finnie1](https://rubytalk.org/u/Daniel_Finnie1)\
**Post date:** [17 December 2006 03:04 UTC](https://rubytalk.org/t/trapping-error-not-caught-by-rescue-exception-how-to/33797/2 "2006-12-17T03:04:32Z")

</div>

Ruby doesn't hang when I run that code.

This is the output:  
daniel@daniel-desktop:~$ ruby /tmp/tmp.rb  
expr=5\*\*2, expr.class=String  
eval(expr)=25  
expr=date, expr.class=String  
ERROR: undefined local variable or method `date' for main:Object  
ERROR: undefined method `strip!' for nil:NilClass  
expr=y, expr.class=String  
ERROR: undefined local variable or method `y' for main:Object  
expr=`date`, expr.class=String  
eval(expr)=Sat Dec 16 22:01:37 EST 2006

Richard wrote:

> **···**
>
> > Hi,
> > 
> > The fourth data line in the program below hangs rather than submitting  
> > to the rescue clause. How can I trap that kind of error?
> > 
> > I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, MySQL  
> > 5.0.15-nt, SciTE 1.59
> > 
> > TIA,  
> > Richard
> > 
> > === code start ===  
> > begin  
> > &nbsp;&nbsp;DATA.each do |line|  
> > &nbsp;&nbsp;&nbsp;&nbsp;var, expr, excess = line.chomp.split(/=/, 3)  
> > &nbsp;&nbsp;&nbsp;&nbsp;var.strip!; expr.strip!  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "expr=" + expr + ", expr.class=" +expr.class.to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "eval(expr)=" + eval(expr).to\_s  
> > &nbsp;&nbsp;end  
> > rescue Exception =\> eMsg  
> > &nbsp;&nbsp;puts "ERROR: " + eMsg  
> > &nbsp;&nbsp;retry  
> > end
> > 
> > \_\_END\_\_  
> > a = 5\*\*2 =# 25  
> > b =date =# undefined local variable or method `date' for  
> > main:Object  
> > c = y =# undefined local variable or method `y' for main:Object  
> > c =`date` =# (ruby hangs; Ctrl-C kills it)  
> > === code end ===

---

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [17 December 2006 04:05 UTC](https://rubytalk.org/t/trapping-error-not-caught-by-rescue-exception-how-to/33797/3 "2006-12-17T04:05:09Z")

</div>

Hi Daniel,

Thanks for your results. I suspect you were running on Uni\*. I was  
running in SciTE over WindowsXP.

When I ran the program in a Command window, I saw what the problem is:  
in Windows, the date command waits for keyboard input specifying a new  
system date. That wasn't apparent when I was running under SciTE.

So, all's well in my Ruby sandbox.

Regards,  
Richard

Daniel Finnie wrote:

> **···**
>
> > Ruby doesn't hang when I run that code.
> > 
> > This is the output:  
> > daniel@daniel-desktop:~$ ruby /tmp/tmp.rb  
> > expr=5\*\*2, expr.class=String  
> > eval(expr)=25  
> > expr=date, expr.class=String  
> > ERROR: undefined local variable or method `date' for main:Object  
> > ERROR: undefined method `strip!' for nil:NilClass  
> > expr=y, expr.class=String  
> > ERROR: undefined local variable or method `y' for main:Object  
> > expr=`date`, expr.class=String  
> > eval(expr)=Sat Dec 16 22:01:37 EST 2006
> > 
> > Richard wrote:  
> > \> Hi,  
> > \>  
> > \> The fourth data line in the program below hangs rather than submitting  
> > \> to the rescue clause. How can I trap that kind of error?  
> > \>  
> > \> I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, MySQL  
> > \> 5.0.15-nt, SciTE 1.59  
> > \>  
> > \> TIA,  
> > \> Richard  
> > \>  
> > \> === code start ===  
> > \> begin  
> > \> DATA.each do |line|  
> > \> var, expr, excess = line.chomp.split(/=/, 3)  
> > \> var.strip!; expr.strip!  
> > \> puts "expr=" + expr + ", expr.class=" +expr.class.to\_s  
> > \> puts "eval(expr)=" + eval(expr).to\_s  
> > \> end  
> > \> rescue Exception =\> eMsg  
> > \> puts "ERROR: " + eMsg  
> > \> retry  
> > \> end  
> > \>  
> > \> \_\_END\_\_  
> > \> a = 5\*\*2 =# 25  
> > \> b =date =# undefined local variable or method `date' for  
> > \> main:Object  
> > \> c = y =# undefined local variable or method `y' for main:Object  
> > \> c =`date` =# (ruby hangs; Ctrl-C kills it)  
> > \> === code end ===  
> > \>  
> > \>  
> > \>
