[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Iñaki Baz Castillo

7/20/2008 2:35:00 PM

Hi, I read in this article:
http://jerith.livejournal.com/...
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

15 Answers

Stefano Crocco

7/20/2008 2:41:00 PM

0

On Sunday 20 July 2008, I=C3=B1aki Baz Castillo wrote:
> Hi, I read in this article:
> http://jerith.livejournal.com/...
> 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

Phlip

7/20/2008 2:54:00 PM

0

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

Robert Dober

7/20/2008 2:56:00 PM

0

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


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

Iñaki Baz Castillo

7/20/2008 3:01:00 PM

0

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

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

Phlip

7/20/2008 3:04:00 PM

0

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

Stefano Crocco

7/20/2008 3:09:00 PM

0

On Sunday 20 July 2008, I=C3=B1aki Baz Castillo wrote:
> > 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

Phlip

7/20/2008 3:31:00 PM

0

Stefano Crocco 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

<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

Phlip

7/20/2008 3:37:00 PM

0

> Gotta do this:

rescue
p $!
p $!.message
end

> You might be rescuing something else.

D'oh!

Phlip

7/20/2008 3:42:00 PM

0

> You might be rescuing something else.

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

Iñaki Baz Castillo

7/20/2008 3:42:00 PM

0

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