[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rescue anything raised?

williamerubin

12/8/2005 7:11:00 PM

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.

9 Answers

Caleb Tennis

12/8/2005 7:15:00 PM

0


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


Jeffrey Moss

12/8/2005 7:18:00 PM

0

rescue Exception

All exceptions descend from class Exception.

-Jeff

On Fri, Dec 09, 2005 at 04:12:35AM +0900, William E. Rubin wrote:
> 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.
>
>


williamerubin

12/8/2005 7:25:00 PM

0

Great. Thanks!

Daniel Schierbeck

12/8/2005 8:22:00 PM

0

Jeffrey Moss wrote:
> rescue Exception
>
> All exceptions descend from class Exception.
>
> -Jeff
>
> On Fri, Dec 09, 2005 at 04:12:35AM +0900, William E. Rubin wrote:
>> 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.
>>
>>

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

Wayne Vucenic

12/8/2005 10:55:00 PM

0

Hi Daniel,

On 12/8/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
> Jeffrey Moss wrote:
> > 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

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 Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"


Sean O'Halpin

12/8/2005 11:08:00 PM

0

On 12/8/05, Jeffrey Moss <jeff@opendbms.com> 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


Wayne Vucenic

12/8/2005 11:40:00 PM

0

Hi Sean,

On 12/8/05, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
> On 12/8/05, Jeffrey Moss <jeff@opendbms.com> 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 Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"


Sean O'Halpin

12/9/2005 12:06:00 AM

0

On 12/8/05, Wayne Vucenic <nightphotos@gmail.com> 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. :)

Regards,

Sean


Wayne Vucenic

12/9/2005 12:13:00 AM

0

On 12/8/05, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
> 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