Rescue anything raised?

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

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.

Great. Thanks!

Jeffrey Moss wrote:

rescue Exception

All exceptions descend from class Exception.

-Jeff

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

···

On Fri, Dec 09, 2005 at 04:12:35AM +0900, William E. Rubin wrote:

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

···

On 12/8/05, Jeffrey Moss <jeff@opendbms.com> wrote:

rescue Exception

All exceptions descend from class Exception.

Hi Daniel,

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

···

On 12/8/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:

---
Wayne Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"

Hi Sean,

> 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

···

On 12/8/05, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:

On 12/8/05, Jeffrey Moss <jeff@opendbms.com> wrote:

---
Wayne Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"

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

Regards,

Sean

···

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.

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

···

On 12/8/05, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:

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