rescue anything raised?

  • Thread starter William E. Rubin
  • Start date
W

William E. Rubin

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.
 
C

Caleb Tennis

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"
 
D

Daniel Schierbeck

Jeffrey said:
rescue Exception

All exceptions descend from class Exception.

-Jeff

I actually think that's kinda non-rubyish. Why not simply require
objects to have the basic methods of an exception (`exception', and
maybe `message', `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
 
W

Wayne Vucenic

Hi Daniel,

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

class FooException
attr_reader :message

def initialize(message)
@message =3D message
end

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

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

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

def exception
Exception.new(@message)
end
end

begin
raise FooException.new("I'm a foo exception")
rescue Exception =3D> e
p e
end

which outputs

#<Exception: I'm a foo exception>

Wayne
 
S

Sean O'Halpin

rescue Exception

All exceptions descend from class Exception.
That's not quite true. Compare:

begin
eval "foo *"
rescue Exception =3D> 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 =3D> 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
 
W

Wayne Vucenic

Hi Sean,

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
 
S

Sean O'Halpin

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. :)

Regards,

Sean
 
W

Wayne Vucenic

You're right. Forgive the brainstorm. :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top