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

Hi, I read in this article:
Ruby rant: Timeout::Error - Jerith online — LiveJournal
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.

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

- SyntaxError

will cause a crash. ---------------------------

And I get:

=> “rescued !!!”

So is the above article tru or not?

Thanks.

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

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

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

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

[Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 1.8.6.36-1ubuntu3.1

irb> 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)

¿?

On Sun, Jul 20, 2008 at 4:49 PM, Phlip [email protected] wrote:

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

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

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.

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

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

Stefano

Hi –

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

- Interrupt

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

   raise Interrupt

¿?

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

Gotta do this:

rescue
p $!
p $!.message
end

You might be rescuing something else.

D’oh!

Stefano C. wrote:

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

Stupid polymorphic mixins!

Gotta do this:

rescue
p $1
p $1.message
end

You might be rescuing something else.

You might be rescuing something else.

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

On Sun, Jul 20, 2008 at 5:04 PM, Phlip [email protected] wrote:

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

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


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


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

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.

El Domingo, 20 de Julio de 2008, Stefano C. escribió:

irb> Â 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

Yes, you are right.

Hi –

On Mon, 21 Jul 2008, Phlip wrote:

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 Sun, Jul 20, 2008 at 7:59 PM, Phlip [email protected] 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.
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


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

There’s no one thing that’s true. It’s all true.