Forum: Ruby rescue anything raised?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
williamerubin (Guest)
on 2005-12-08 21:16
(Received via mailing list)
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.
caleb (Guest)
on 2005-12-08 21:16
(Received via mailing list)
> 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"
jeff (Guest)
on 2005-12-08 21:20
(Received via mailing list)
rescue Exception

All exceptions descend from class Exception.

-Jeff
williamerubin (Guest)
on 2005-12-08 21:32
(Received via mailing list)
Great.  Thanks!
Daniel S. (Guest)
on 2005-12-08 22:25
(Received via mailing list)
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
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
nightphotos (Guest)
on 2005-12-09 00:57
(Received via mailing list)
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"
Sean O. (Guest)
on 2005-12-09 01:09
(Received via mailing list)
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
nightphotos (Guest)
on 2005-12-09 01:41
(Received via mailing list)
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"
Sean O. (Guest)
on 2005-12-09 02:06
(Received via mailing list)
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. :)

Regards,

Sean
nightphotos (Guest)
on 2005-12-09 02:14
(Received via mailing list)
On 12/8/05, Sean O'Halpin <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.