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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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

=2D--------------------------
Exception
- StandardError
- RuntimeError
- ZeroDivisionError
- ScriptError
- SyntaxError
- SystemExit
- SignalException
- Interrupt

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

But is it really true? I do:

=2D---
begin
raise Interrupt
rescue
puts "rescued !!!"
end
=2D---

And I get:

=3D> "rescued !!!"


So is the above article tru or not?

Thanks.


=2D-=20
I=C3=B1aki Baz Castillo
 
S

Stefano Crocco

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:

=3D> "rescued !!!"


So is the above article tru or not?

Thanks.

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

Stefano
 
P

Phlip

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!
 
R

Robert Dober

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!


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

Iñaki Baz Castillo

El Domingo, 20 de Julio de 2008, Phlip escribi=C3=B3:
Stefano said:
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

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


It's different!

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

=2D----------
require 'timeout'

begin
raise Interrupt
rescue
puts "rescued !!!"
puts $!
end
=2D---------

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


=C2=BF?



=2D-=20
I=C3=B1aki Baz Castillo
 
P

Phlip

begin
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.
 
S

Stefano Crocco

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

=C2=A0 [Interrupt, SignalException, Exception, Object, Kernel]

ruby1.8 =C2=A0 =C2=A01.8.6.36-1ubuntu3.1

irb> =C2=A0p 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=
=20
starts up, namely wirble.rb and pp.rb

Stefano
 
P

Phlip

You might be rescuing something else.

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

Iñaki Baz Castillo

El Domingo, 20 de Julio de 2008, Stefano Crocco escribi=C3=B3:
irb> =C2=A0p 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.



=2D-=20
I=C3=B1aki Baz Castillo
 
T

Trys

Iñaki Baz Castillo said:
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)

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

Robert Dober

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.
 
P

Phlip

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.
 
D

David A. Black

Hi --

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:
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.
=> 287910

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


David

--
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!
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top