Does "rescue" wihtour argument handle any kind of Exception or not?

Hi, I read in this article:
  http://jerith.livejournal.com/40063.html
that:

···

---------------------------
Exception
  - StandardError
    - RuntimeError
    - ZeroDivisionError
  - ScriptError
    - SyntaxError
  - SystemExit
  - SignalException
    - Interrupt

The important bit there is that all the stuff you can reasonably expect to
recover from is under StandardError. Because of this, a default rescue block
will not catch anything that isn't a StandardError. The observant reader will
notice that I helpfully showed Interrupt's position in the hierarchy. The
observant reader will also notice that it is not a subclass of StandardError.
This means that you need to catch it explicitly, or it will cause a crash.
---------------------------

But is it really true? I do:

----
begin
  raise Interrupt
rescue
  puts "rescued !!!"
end
----

And I get:

=> "rescued !!!"

So is the above article tru or not?

Thanks.

--
Iñaki Baz Castillo

For me, using ruby 1.8.7, patchlevel 22 on Gentoo Linux, your code works as
the article says, that is the exception is not being caught.

Stefano

···

On Sunday 20 July 2008, Iñaki Baz Castillo wrote:

Hi, I read in this article:
  http://jerith.livejournal.com/40063.html
that:

---------------------------
Exception
  - StandardError
    - RuntimeError
    - ZeroDivisionError
  - ScriptError
    - SyntaxError
  - SystemExit
  - SignalException
    - Interrupt

The important bit there is that all the stuff you can reasonably expect to
recover from is under StandardError. Because of this, a default rescue
block will not catch anything that isn't a StandardError. The observant
reader will notice that I helpfully showed Interrupt's position in the
hierarchy. The observant reader will also notice that it is not a subclass
of StandardError. This means that you need to catch it explicitly, or it
will cause a crash. ---------------------------

But is it really true? I do:

----
begin
  raise Interrupt
rescue
  puts "rescued !!!"
end
----

And I get:

=> "rescued !!!"

So is the above article tru or not?

Thanks.

Stefano Crocco wrote:

Exception
  - StandardError
    - RuntimeError
    - ZeroDivisionError
  - ScriptError
    - SyntaxError
  - SystemExit
  - SignalException
    - Interrupt

begin
  raise Interrupt
rescue
  puts "rescued !!!"
end

For me, using ruby 1.8.7, patchlevel 22 on Gentoo Linux, your code works as the article says, that is the exception is not being caught.

Same with p22 on Ubuntu. (I think I compiled it, too.)

Run p Interrupt.ancestors, and see if you get:

  [Interrupt, SignalException, Exception, Object, Kernel]

Also, put p $! into the rescue handler to see _what_ you rescued!

···

--
   Phlip

Did you not mean "the exception was caught" instead of "not being caught" ?
Robert

···

On Sun, Jul 20, 2008 at 4:49 PM, Phlip <phlip2005@gmail.com> wrote:

Stefano Crocco wrote:

Exception
- StandardError
   - RuntimeError
   - ZeroDivisionError
- ScriptError
   - SyntaxError
- SystemExit
- SignalException
   - Interrupt

begin
       raise Interrupt
rescue
       puts "rescued !!!"
end

For me, using ruby 1.8.7, patchlevel 22 on Gentoo Linux, your code works
as the article says, that is the exception is not being caught.

Same with p22 on Ubuntu. (I think I compiled it, too.)

Run p Interrupt.ancestors, and see if you get:

[Interrupt, SignalException, Exception, Object, Kernel]

Also, put p $! into the rescue handler to see _what_ you rescued!

--
Phlip

Stefano Crocco wrote:
>> Exception
>> - StandardError
>> - RuntimeError
>> - ZeroDivisionError
>> - ScriptError
>> - SyntaxError
>> - SystemExit
>> - SignalException
>> - Interrupt
>>
>> begin
>> raise Interrupt
>> rescue
>> puts "rescued !!!"
>> end
>
> For me, using ruby 1.8.7, patchlevel 22 on Gentoo Linux, your code works
> as the article says, that is the exception is not being caught.

Same with p22 on Ubuntu. (I think I compiled it, too.)

Run p Interrupt.ancestors, and see if you get:

  [Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 1.8.6.36-1ubuntu3.1

p Interrupt.ancestors

[Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
PP::ObjectMixin, Kernel]

It's different!

···

El Domingo, 20 de Julio de 2008, Phlip escribió:

Also, put p $! into the rescue handler to see _what_ you rescued!

-----------
require 'timeout'

begin
        raise Interrupt
rescue
        puts "rescued !!!"
        puts $!
end
----------

~# ./test.rb
rescued !!!
wrong number of arguments (0 for 1)

¿?

--
Iñaki Baz Castillo

begin
       raise Interrupt
rescue
       puts "rescued !!!"
end

Did you not mean "the exception was caught" instead of "not being caught" ?
Robert

On Ubuntu, with p22, the Interrupt was not rescued, and it blew my program away.

Hi --

Stefano Crocco wrote:

Exception
  - StandardError
    - RuntimeError
    - ZeroDivisionError
  - ScriptError
    - SyntaxError
  - SystemExit
  - SignalException
    - Interrupt

begin
  raise Interrupt
rescue
  puts "rescued !!!"
end

For me, using ruby 1.8.7, patchlevel 22 on Gentoo Linux, your code works
as the article says, that is the exception is not being caught.

Same with p22 on Ubuntu. (I think I compiled it, too.)

Run p Interrupt.ancestors, and see if you get:

  [Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 1.8.6.36-1ubuntu3.1

> p Interrupt.ancestors
[Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
PP::ObjectMixin, Kernel]

It's different!

Also, put p $! into the rescue handler to see _what_ you rescued!

-----------
require 'timeout'

begin
       raise Interrupt
rescue
       puts "rescued !!!"
       puts $!
end
----------

~# ./test.rb
rescued !!!
wrong number of arguments (0 for 1)

¿?

begin
   raise Interrupt
rescue => e
   puts "rescued !!!: #{e.class}"
end

It's ArgumentError in 1.8.6, and nothing (the rescue doesn't happen)
in 1.9.

David

···

On Mon, 21 Jul 2008, Iñaki Baz Castillo wrote:

El Domingo, 20 de Julio de 2008, Phlip escribió:

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

The two extra ancestors come from files you most likely auto-load when irb
starts up, namely wirble.rb and pp.rb

Stefano

···

On Sunday 20 July 2008, Iñaki Baz Castillo wrote:

> Run p Interrupt.ancestors, and see if you get:
>
> [Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 1.8.6.36-1ubuntu3.1

> p Interrupt.ancestors
[Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
PP::ObjectMixin, Kernel]

It's different!

Iñaki Baz Castillo wrote:

Run p Interrupt.ancestors, and see if you get:

   [Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 1.8.6.36-1ubuntu3.1

> p Interrupt.ancestors
[Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
PP::ObjectMixin, Kernel]

It's different!

Also, put p $! into the rescue handler to see _what_ you rescued!

-----------
require 'timeout'

begin
         raise Interrupt
rescue
         puts "rescued !!!"
         puts $!
end
----------

~# ./test.rb
rescued !!!
wrong number of arguments (0 for 1)

Right here is your problem, same thing is happening to me (ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]) For certain reasons raise throws ArgumentError (which then gets caught by single rescue) for Interrupt without additional parameters.

This will probably work if you just need to raise Interrupt:

raise Interrupt, "an exception"

Stefano Crocco wrote:

> p Interrupt.ancestors
[Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
PP::ObjectMixin, Kernel]

It's different!

It's the same! StandardError is not on the list.

The two extra ancestors come from files you most likely auto-load when irb starts up, namely wirble.rb and pp.rb

<voice impersonation='Homer Simpson'>
   Stupid polymorphic mixins!
</voice>

Gotta do this:

   rescue
      p $1
      p $1.message
   end

You might be rescuing something else.

···

--
   Phlip

Yes, you are right.

···

El Domingo, 20 de Julio de 2008, Stefano Crocco escribió:

> > p Interrupt.ancestors
> [Interrupt, SignalException, Exception, Object, Wirble::Shortcuts,
> PP::ObjectMixin, Kernel]
>
>
> It's different!

The two extra ancestors come from files you most likely auto-load when irb
starts up, namely wirble.rb and pp.rb

--
Iñaki Baz Castillo

Did you expect your program to do anything after printing "rescued !!!" ?
R.

···

On Sun, Jul 20, 2008 at 5:04 PM, Phlip <phlip2005@gmail.com> wrote:

begin
      raise Interrupt
rescue
      puts "rescued !!!"
end

Did you not mean "the exception was caught" instead of "not being caught"
?
Robert

On Ubuntu, with p22, the Interrupt was not rescued, and it blew my program
away.

--
http://ruby-smalltalk.blogspot.com/

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

Gotta do this:

   rescue
      p $!
      p $!.message
    end

You might be rescuing something else.

D'oh!

Did you expect your program to do anything after printing "rescued !!!" ?

Assuming you are addressing a senior enjinere, not a neophyte, yes I could tell the difference between an interrupt and the rest of my program running. It has major side-effects; I stuck the sample code at the top of my current project.

Curiously, this is what the original code was doing:

begin
   raise Interrupt
rescue Object
   p $!.class # sez Interrupt
end

I didn't catch it either - 'raise' raises the given object, which here was class Interrupt, and the metaclass itself of course does not inherit StandardError.

Try:

begin
   raise Interrupt.new('L.A. woman')
rescue
   p $!.class
end

That does not invoke the rescue, and does clobber the program, all for the correct reasons. And rescue Interrupt catches it.

···

--
   Phlip

You might be rescuing something else.

Also, write a little sample program. It might behave different from IRB.

Hi --

Did you expect your program to do anything after printing "rescued !!!" ?

Assuming you are addressing a senior enjinere, not a neophyte, yes I could tell the difference between an interrupt and the rest of my program running. It has major side-effects; I stuck the sample code at the top of my current project.

Curiously, this is what the original code was doing:

begin
raise Interrupt
rescue Object
p $!.class # sez Interrupt

I get ArgumentError with 1.8.6 and Interrupt with 1.9. I'm not sure
why Interrupt needs an argument in 1.8.6.

end

I didn't catch it either - 'raise' raises the given object, which here was class Interrupt, and the metaclass itself of course does not inherit StandardError.

raise doesn't necessarily raise the object itself. It looks for an
'exception' method on the object:

obj = Object.new

=> #<Object:0xb1bc>

def obj.exception; RuntimeError.new("Problem"); end

=> nil

raise obj

RuntimeError: Problem

Instances of Exception (and subclasses) return themselves from
#exception, so in that case you would be raising the object itself.
The classes return new instances of themselves.

r = RuntimeError.exception

=> #<RuntimeError: RuntimeError>

r.object_id

=> 287910

r.exception.object_id

=> 287910

If you give #exception on an instance a new message to deliver, and
you get a new instance:

r.exception("Problem").object_id

=> 268680

David

···

On Mon, 21 Jul 2008, Phlip wrote:

--
Rails training from David A. Black and Ruby Power and Light:
     Intro to Ruby on Rails July 21-24 Edison, NJ
  * Advancing With Rails August 18-21 Edison, NJ
  * Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!

Philip you said that it was so, by confirming an output that indicated
the contrary. As a hopeless formalist I trusted the output more than
what you were writing in informal language, my error, I suppose.
BTW Philip you should never assume, you know why of course ;).

Cheers
Robert

···

On Sun, Jul 20, 2008 at 7:59 PM, Phlip <phlip2005@gmail.com> wrote:

Did you expect your program to do anything after printing "rescued !!!" ?

Assuming you are addressing a senior enjinere, not a neophyte, yes I could
tell the difference between an interrupt and the rest of my program running.
It has major side-effects; I stuck the sample code at the top of my current
project.

--
http://ruby-smalltalk.blogspot.com/

There's no one thing that's true. It's all true.
--
Ernest Hemingway