Rescue anything raised?


#1

Is there a way to rescue any raised error? Like “rescue *” or
something?

The “Programming Ruby” book on ruby-lang.org says that just plain
“rescue” (without a parameter list) will rescue any “StandardError”,
but it doesn’t go on to say that anything raised is necessarily
descended from StandardError, or to explicitly mention a way to rescue
everything.

I was guessing that just plain “rescue” might work, but this doesn’t
seem to be true - my code just got a “Timeout::Error”, and it was not
rescued by a parameterless “rescue”.

Thanks.


#2

I was guessing that just plain “rescue” might work, but this doesn’t
seem to be true - my code just got a “Timeout::Error”, and it was not
rescued by a parameterless “rescue”.

Try: “rescue Exception”


#3

rescue Exception

All exceptions descend from class Exception.

-Jeff


#4

Great. Thanks!


#5

Jeffrey M. wrote:

The “Programming Ruby” book on ruby-lang.org says that just plain

I actually think that’s kinda non-rubyish. Why not simply require
objects to have the basic methods of an exception (exception', and maybemessage’, `backtrace’, etc.) to qualify as an exception?

class FooException
attr_reader :message

 def initialize(message)
   @message = message
 end

 def exception(message = nil)
   if message.nil?
     return self
   else
     self.new(message)
   end
 end

end

Cheers,
Daniel


#6

On 12/8/05, Jeffrey M. removed_email_address@domain.invalid wrote:

rescue Exception

All exceptions descend from class Exception.

That’s not quite true. Compare:

begin
eval “foo *”
rescue Exception => e
p e
end

– OUTPUT –
#<SyntaxError: (eval):1: compile error
(eval):1: syntax error
foo *
^>

versus

begin
eval “foo *”
rescue SyntaxError
puts “syntax error”
rescue Exception => e
p e
end

– OUTPUT –
syntax error

There is a forest of exceptions not a tree. To capture an exception
generally, you have to specify which family you want to intercept. The
top level families of exception are: NoMemoryError, ScriptError,
SignalException, StandardError, SystemExit and SystemStackError.

You can find the Exception class hierarchy in Pickaxe 2, p. 109.

Regards,

Sean


#7

Hi Daniel,

On 12/8/05, Daniel S. removed_email_address@domain.invalid wrote:

  else
    self.new(message)
  end
end

end

You can raise any object whose exception method returns an Exception.
So your example would become:

class FooException
def initialize(message)
@message = message
end

def exception
Exception.new(@message)
end
end

begin
raise FooException.new(“I’m a foo exception”)
rescue Exception => e
p e
end

which outputs

#<Exception: I’m a foo exception>

Wayne


Wayne V.
No Bugs Software
“Ruby and C++ Agile Contract Programming in Silicon Valley”


#8

Hi Sean,

On 12/8/05, Sean O’Halpin removed_email_address@domain.invalid wrote:

On 12/8/05, Jeffrey M. removed_email_address@domain.invalid wrote:

rescue Exception

All exceptions descend from class Exception.

That’s not quite true…
There is a forest of exceptions not a tree. To capture an exception
generally, you have to specify which family you want to intercept.

I don’t think that’s correct. Exceptions do form a tree, and rescuing
“Exception” will rescue all exceptions. I found your examples a
little confusing because you’re doing different things in the rescue
clauses. If we change the rescue clauses to be the same:

begin
eval “foo *”
rescue SyntaxError
puts “I got here”
end

versus

begin
eval “foo *”
rescue Exception
puts “I got here”
end

The output is the same in both cases, “I got here”.

Take care,

Wayne


Wayne V.
No Bugs Software
“Ruby and C++ Agile Contract Programming in Silicon Valley”


#9

On 12/8/05, Wayne V. removed_email_address@domain.invalid wrote:

Hi Sean,
I don’t think that’s correct. Exceptions do form a tree, and rescuing
“Exception” will rescue all exceptions. I found your examples a
little confusing because you’re doing different things in the rescue
clauses.

You’re right. Forgive the brainstorm. :slight_smile:

Regards,

Sean


#10

On 12/8/05, Sean O’Halpin removed_email_address@domain.invalid wrote:

You’re right. Forgive the brainstorm. :slight_smile:

No problem. It made me take a closer look at the Exception hierarchy
and think about how it works, which is a good thing.

Take care,

Wayne