Which exception classes does "rescue" trap?

Hi, using Ruby 1.9 I’ve realized that “rescue” traps more than
StandardError exceptions, for example NoMemoryError:

irb> NoMemoryError.ancestors
[NoMemoryError, Exception, Object, PP::ObjectMixin, Kernel, BasicObject]

begin
raise NoMemoryError, “NO MEMORY !!!”
rescue
puts “rescued”
end

=> rescued

But for example it does not trap ScriptError exceptions. So which
exact exception classes does “rescue” trap? is it docummented
somewhere? Unfortunately I am not able to find in Ruby sources where
the definition of “rescue” is.

Thanks a lot.

On 17/05/2012, at 9:59 AM, Iaki Baz C. wrote:

Hi, using Ruby 1.9 I’ve realized that “rescue” traps more than
StandardError exceptions, for example NoMemoryError:

Not for me it doesn’t.

irb
1.9.3p125 :001 > NoMemoryError.ancestors
=> [NoMemoryError, Exception, Object, Kernel, BasicObject]
1.9.3p125 :002 >
1.9.3p125 :003 > begin
1.9.3p125 :004 > raise NoMemoryError, “NO MEMORY !!!”
1.9.3p125 :005?> rescue
1.9.3p125 :006?> puts “rescued”
1.9.3p125 :007?> end
NoMemoryError: NO MEMORY !!!
from (irb):4
from /Users/henry/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `’
1.9.3p125 :008 >

But for example it does not trap ScriptError exceptions. So which
exact exception classes does “rescue” trap? is it docummented
somewhere? Unfortunately I am not able to find in Ruby sources where
the definition of “rescue” is.

Google is your friend…

http://jpablobr.com/past/ruby-s-exception-hierarchy

Rescuing an Exception class will also rescue it’s sub-classes. This
doesn’t work work for modules though, which would be really handy.

Henry

2012/5/17 Henry M. [email protected]:

On 17/05/2012, at 9:59 AM, Iñaki Baz C. wrote:

Hi, using Ruby 1.9 I’ve realized that “rescue” traps more than
StandardError exceptions, for example NoMemoryError:

Not for me it doesn’t.

My fault, sorry, I did a wrong code.

But for example it does not trap ScriptError exceptions. So which
exact exception classes does “rescue” trap? is it docummented
somewhere? Unfortunately I am not able to find in Ruby sources where
the definition of “rescue” is.

Google is your friend…

http://jpablobr.com/past/ruby-s-exception-hierarchy

Rescuing an Exception class will also rescue it’s sub-classes. This doesn’t work
work for modules though, which would be really handy.

Yes sure :slight_smile:

My question is, however: does “rescue” trap any exception class not
inheriting from StandardError?

But don’t worry, I’m coding some script right now to discover it :slight_smile:

Thanks a lot.

2012/5/17 Iñaki Baz C. [email protected]:

My question is, however: does “rescue” trap any exception class not
inheriting from StandardError?

Code done. If I’m not wrong, “rescue” traps the following exception
classes (and of course all the sub-classes):

  • SignalException
  • StandardError

On 17/05/2012, at 10:45 AM, Iaki Baz C. wrote:

But don’t worry, I’m coding some script right now to discover it :slight_smile:

Here’s one prepared earlier

http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy

Henry

On 17/05/2012, at 10:56 AM, Iaki Baz C. wrote:

2012/5/17 Iaki Baz C. [email protected]:

My question is, however: does “rescue” trap any exception class not
inheriting from StandardError?

Code done. If I’m not wrong, “rescue” traps the following exception
classes (and of course all the sub-classes):

  • SignalException
  • StandardError

So it does. That’s unexpected.

Henry

Hi,

SignalExceptions are not rescued. You probably used a wrong argument
for the signal and caused an ArgumentError, which is then rescued:

begin
raise SignalException, ‘foo’
rescue => error
p “#{error.class}: #{error.message}” # error is not a
SignalException
end

According to the “IPA Ruby Standardization WG Draft August 25, 2010”

(page 97)
11.5.2.5 The begin expression
Semantics
c.2
“If the exception-class-list is omitted in the rescue-clause, and if E
is an instance of the class StandardError, the rescue-clause handles E.”

If any Ruby interpreter handles any exception that is not a sub-class of
StandardError in a rescue-clause (no exception-class-list), then it is
in error.


It’s not an answer without a reference citation.

On 17/05/2012, at 12:02 PM, Jan E. wrote:

Hi,

SignalExceptions are not rescued. You probably used a wrong argument
for the signal and caused an ArgumentError, which is then rescued:

Correct. That makes more sense.

Henry

2012/5/17 Llelan D. [email protected]:

StandardError in a rescue-clause (no exception-class-list), then it is
in error.

Clear and confirmed now. Thanks a lot.

2012/5/17 Jan E. [email protected]:

SignalExceptions are not rescued. You probably used a wrong argument
for the signal and caused an ArgumentError, which is then rescued:

You are right:

begin
raise SignalException
rescue => e
puts “Exception #{e.inspect} rescued”
end

Output:

Exception #<ArgumentError: wrong number of arguments (0 for 1)> rescued

It seems that SingalException.new requires one argument.

Thanks a lot.