# How to rescue any exception in 1.8?

**URL:** <https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042>\
**Category:** ruby-talk\
**Created:** [26 July 2003 20:50 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042 "2003-07-26T20:50:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Mak1](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Philip\_Mak1](https://rubytalk.org/u/Philip_Mak1)\
**Post date:** [26 July 2003 20:50 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/1 "2003-07-26T20:50:32Z")

</div>

This code will catch the exception in Ruby 1.6:

require 'timeout’  
begin  
raise TimeoutError  
rescue  
puts "Rescued!"  
end

But in Ruby 1.8, it will not catch the exception. I found out that if  
I do “rescue Exception” instead of just “rescue”, it will work.

So, my question is, if I want to rescue any exception no matter what  
it is, do I have to start typing “rescue Exception” instead of  
"rescue"?

---

<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:** [26 July 2003 21:23 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/2 "2003-07-26T21:23:48Z")

</div>

In general, you always did:

$ ruby16 -ve ‘begin; require “flurble”; rescue; puts “ok”; end’  
ruby 1.6.8 (2002-12-24) [i386-freebsd4.8]  
-e:1:in `require’: No such file to load – flurble (LoadError)  
from -e:1

$ ruby16 -ve ‘begin; require “flurble”; rescue Exception; puts “ok”; end’  
ruby 1.6.8 (2002-12-24) [i386-freebsd4.8]  
ok

‘rescue’ by itself is like ‘rescue StandardError’, I believe (which catches  
StandardError and all its subclasses)

However it does look like timeout exceptions have changed from 1.6.8 to 1.8

1.6.8: raised TimeoutError, which was a subclass of StandardError  
1.8.0: raises Timeout::Error, which is a subclass of Interrupt

However in 1.8.0 you can pass in an exception class, so you can do:

require ‘timeout’  
begin  
timeout(3, StandardError) do  
sleep 5  
end  
rescue  
puts “ok”  
end

Or, to make your code compatible with both versions, without trapping  
absolutely every exception, you can do

rescue StandardError, Interrupt

Cheers,

Brian.

> **···**
>
> On Sun, Jul 27, 2003 at 05:50:32AM +0900, Philip Mak wrote:
> 
> > This code will catch the exception in Ruby 1.6:
> > 
> > require ‘timeout’  
> > begin  
> > raise TimeoutError  
> > rescue  
> > puts “Rescued!”  
> > end
> > 
> > But in Ruby 1.8, it will not catch the exception. I found out that if  
> > I do “rescue Exception” instead of just “rescue”, it will work.
> > 
> > So, my question is, if I want to rescue any exception no matter what  
> > it is, do I have to start typing “rescue Exception” instead of  
> > “rescue”?

---

<div class="post-metadata">

**Author:** ![Michael\_Granger](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Michael\_Granger](https://rubytalk.org/u/Michael_Granger)\
**Post date:** [26 July 2003 21:30 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/3 "2003-07-26T21:30:32Z")

</div>

Yes, but that’s not a change in the way rescue works: it’s been like  
that since 1.6.x at least. For example, a begin/rescue/end under 1.6.x  
doesn’t catch Interrupts or ScriptErrors, but does catch RuntimeErrors:

$ ruby-1.6.8 -de ‘begin; sleep 15; rescue =\> err;   
puts “Rescued #{err.class.name}”; end’  
Exception `Interrupt' at -e:1 - -e:1:in `sleep’: Interrupt  
from -e:1

$ ruby-1.6.8 -de ‘begin; slep 15; rescue =\> err;   
puts “Rescued #{err.class.name}”; end’  
Exception `NameError' at -e:1 - undefined method \ `slep’ for #Object:0x40289ce0  
-e:1: undefined method `slep’ for #Object:0x40289ce0 (NameError)

$ ruby-1.6.8 -de ‘begin; raise RuntimeError, “foo!”;  
rescue =\> err; puts “Rescued #{err.class.name}”; end’  
Exception `RuntimeError’ at -e:1 - foo!  
Rescued RuntimeError

> **···**
>
> On Saturday, Jul 26, 2003, at 13:50 US/Pacific, Philip Mak wrote:
> 
> > This code will catch the exception in Ruby 1.6:
> > 
> > require ‘timeout’  
> > begin  
> > raise TimeoutError  
> > rescue  
> > puts “Rescued!”  
> > end
> > 
> > But in Ruby 1.8, it will not catch the exception. I found out that if  
> > I do “rescue Exception” instead of just “rescue”, it will work.
> > 
> > So, my question is, if I want to rescue any exception no matter what  
> > it is, do I have to start typing “rescue Exception” instead of  
> > “rescue”?
> 
> –  
> Michael Granger [ged@FaerieMUD.org](mailto:ged@FaerieMUD.org)  
> Rubymage, Believer, Architect  
> The FaerieMUD Consortium [http://www.FaerieMUD.org/](http://www.FaerieMUD.org/)

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [27 July 2003 05:00 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/4 "2003-07-27T05:00:20Z")

</div>

Hi,

> But in Ruby 1.8, it will not catch the exception. I found out that if  
> I do “rescue Exception” instead of just “rescue”, it will work.

Use “rescue TimeoutError”, if you want to honor backward  
compatibility.

> So, my question is, if I want to rescue any exception no matter what  
> it is, do I have to start typing “rescue Exception” instead of  
> “rescue”?

Yes.

> **···**
>
> At Sun, 27 Jul 2003 05:50:32 +0900, Philip Mak wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [30 July 2003 02:04 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/5 "2003-07-30T02:04:13Z")

</div>

Philip Mak wrote:

> This code will catch the exception in Ruby 1.6:
> 
> require ‘timeout’  
> begin  
> raise TimeoutError  
> rescue  
> puts “Rescued!”  
> end
> 
> But in Ruby 1.8, it will not catch the exception. I found out that if  
> I do “rescue Exception” instead of just “rescue”, it will work.
> 
> So, my question is, if I want to rescue any exception no matter what  
> it is, do I have to start typing “rescue Exception” instead of  
> “rescue”?

Yes.

Rescue without an Exception type only catches StandardError and  
subclasses. The inheritance hierarchy of exceptions are one of the  
changes between 1.6 and 1.8, IIRC…

[kentda@v052a kentda]$ ruby -v -e “require ‘timeout’;p  
TimeoutError.ancestors”  
ruby 1.6.8 (2002-12-24) [i386-linux-gnu]  
[TimeoutError, StandardError, Exception, Object, Kernel]

[kentda@v052a kentda]$ /usr/local/bin/ruby -v -e “require ‘timeout’;p  
TimeoutError.ancestors”  
ruby 1.8.0 (2003-07-24) [i686-linux]  
[Timeout::Error, Interrupt, SignalException, Exception, Object, Kernel]

HTH

> **···**
>
> –  
> ([Kent Dahl]/)\_ _~__**[[http://www.pvv.org/~kentda/](http://www.pvv.org/~kentda/) ]**_/~  
> ))\_student\_/(( \__d L b_\_/ (pre-) Master of Science in Technology )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<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:** [26 July 2003 22:11 UTC](https://rubytalk.org/t/how-to-rescue-any-exception-in-1-8/7042/6 "2003-07-26T22:11:34Z")

</div>

Unfortunately the on-line version of the Pickaxe book doesn’t have the image  
which shows the exception hierarchy. There are probably a zillion programs  
for dumping this out, but here’s another one anyway:

require ‘timeout’

class ObjTree  
def initialize  
@children = Hash.new([])  
ObjectSpace.each\_object(Class) do |c|  
@children[c.superclass] += [c]  
end  
end

def dumptree(klass, level = 0)  
puts " "\*level + “” + klass.to\_s  
@children[klass].sort {|x,y| x.to\_s\<=\>y.to\_s}.each do |c|  
dumptree(c, level+1)  
end  
end  
end

ObjTree.new.dumptree(Exception) # or try Object
