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

Discussion in 'Ruby' started by Iñaki Baz Castillo, Jul 20, 2008.

  1. 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
     
    Iñaki Baz Castillo, Jul 20, 2008
    #1
    1. Advertising

  2. Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    On Sunday 20 July 2008, I=C3=B1aki 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:
    >
    > =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
     
    Stefano Crocco, Jul 20, 2008
    #2
    1. Advertising

  3. Iñaki Baz Castillo

    Phlip Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    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
     
    Phlip, Jul 20, 2008
    #3
  4. Iñaki Baz Castillo

    Robert Dober Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    On Sun, Jul 20, 2008 at 4:49 PM, Phlip <> 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
     
    Robert Dober, Jul 20, 2008
    #4
  5. Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    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
     
    Iñaki Baz Castillo, Jul 20, 2008
    #5
  6. Iñaki Baz Castillo

    Phlip Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    >>>> 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.
     
    Phlip, Jul 20, 2008
    #6
  7. Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    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
     
    Stefano Crocco, Jul 20, 2008
    #7
  8. Iñaki Baz Castillo

    Phlip Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    > Gotta do this:

    rescue
    p $!
    p $!.message
    end

    > You might be rescuing something else.


    D'oh!
     
    Phlip, Jul 20, 2008
    #8
  9. Iñaki Baz Castillo

    Phlip Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    > You might be rescuing something else.

    Also, write a little sample program. It might behave different from IRB.
     
    Phlip, Jul 20, 2008
    #9
  10. Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    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
     
    Iñaki Baz Castillo, Jul 20, 2008
    #10
  11. Iñaki Baz Castillo

    Trys Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    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
    >
    > 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"
     
    Trys, Jul 20, 2008
    #11
  12. Iñaki Baz Castillo

    Robert Dober Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    On Sun, Jul 20, 2008 at 5:04 PM, Phlip <> 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.
    >
    >

    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
     
    Robert Dober, Jul 20, 2008
    #12
  13. Iñaki Baz Castillo

    Phlip Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    > 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
     
    Phlip, Jul 20, 2008
    #13
  14. Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    Hi --

    On Mon, 21 Jul 2008, Phlip 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.
    >
    > 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

    --
    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!
     
    David A. Black, Jul 20, 2008
    #14
  15. Iñaki Baz Castillo

    Robert Dober Guest

    Re: Does "rescue" wihtour argument handle any kind of Exception ornot?

    On Sun, Jul 20, 2008 at 7:59 PM, Phlip <> 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.
    --
    Ernest Hemingway
     
    Robert Dober, Jul 21, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Mak
    Replies:
    3
    Views:
    132
    Brian Candler
    Jul 26, 2003
  2. Wes Gamble
    Replies:
    1
    Views:
    113
    Robert Klemme
    Nov 20, 2006
  3. Richard
    Replies:
    2
    Views:
    110
    Richard
    Dec 17, 2006
  4. Ben Vishny

    Rescue Block does not work

    Ben Vishny, Feb 7, 2010, in forum: Ruby
    Replies:
    0
    Views:
    96
    Ben Vishny
    Feb 7, 2010
  5. RichardOnRails
    Replies:
    4
    Views:
    198
    RichardOnRails
    Sep 27, 2010
Loading...

Share This Page