From: Brian Candler Date: 2003-07-27T06:23:48+09:00 Subject: Re: How to rescue any exception in 1.8? 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"? 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.