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.
···
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”?
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
Rubymage, Believer, Architect
The FaerieMUD Consortium http://www.FaerieMUD.org/
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
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/ ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
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