Help begin/rescue/ensure

Discussion in 'Ruby' started by S Kanakakorn, Feb 9, 2007.

  1. S Kanakakorn

    S Kanakakorn Guest

    I have this simple code.

    require 'rubygems'
    require 'net/ssh'

    begin
    session = Net::SSH.start('aaaa-lnx', :password=>'xyz',
    :username=>'root',:timeout=>5)
    rescue
    puts 'cannot connect '
    ensure
    puts 'reach ensure point'
    end
    puts 'at last'

    --------------------------------------

    If I put in non existing host name (aaaa-lnx), then the statement
    "puts 'at last'" won't print out.
    The ensure blocks work fine. Somehow, the "rescue" block can't catch
    the error. This is my output.

    reach ensure point
    /Applications/Locomotive2/Bundles/standardRailsJan2007.locobundle/i386/lib/ruby/1.8/timeout.rb:54:in
    `open': execution expired (Timeout::Error)
    from /Applications/Locomotive2/Bundles/standardRailsJan2007.locobundle/i386/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/transport/session.rb:88:in
    `initialize'

    If I put in a good hostname with wrong password, the "rescue" block
    will catch the error just fine. Here is the output:

    cannot connect
    reach ensure point
    at last


    What did I do wrong ?

    Thanks,
    -Nick
     
    S Kanakakorn, Feb 9, 2007
    #1
    1. Advertising

  2. On Fri, Feb 09, 2007 at 02:20:30PM +0900, S Kanakakorn wrote:
    > require 'rubygems'
    > require 'net/ssh'
    >
    > begin
    > session = Net::SSH.start('aaaa-lnx', :password=>'xyz',
    > :username=>'root',:timeout=>5)
    > rescue
    > puts 'cannot connect '
    > ensure
    > puts 'reach ensure point'
    > end
    > puts 'at last'

    ...
    > What did I do wrong ?


    A bare "rescue" doesn't catch all exceptions, only those which are
    subclasses of StandardError. Change to:

    rescue Exception

    HTH,

    Brian.
     
    Brian Candler, Feb 9, 2007
    #2
    1. Advertising

  3. S Kanakakorn

    ChrisH Guest

    On Feb 9, 12:20 am, "S Kanakakorn" <> wrote:
    > I have this simple code.
    >

    ...
    > rescue
    > puts 'cannot connect '
    > ensure
    > puts 'reach ensure point'
    > end
    > puts 'at last'
    >
    > --------------------------------------
    >
    > If I put in non existing host name (aaaa-lnx), then the statement
    > "puts 'at last'" won't print out.
    > The ensure blocks work fine. Somehow, the "rescue" block can't catch
    > the error. This is my output.
    >
    > reach ensure point
    > /Applications/Locomotive2/Bundles/standardRailsJan2007.locobundle/i386/lib/ruby/1.8/timeout.rb:54:in
    > `open': execution expired (Timeout::Error)
    > from /Applications/Locomotive2/Bundles/standardRailsJan2007.locobundle/i386/lib/ruby/gems/1.8/gems/net-ssh-1.0.10/lib/net/ssh/transport/session.rb:88:in
    > `initialize'
    >
    > If I put in a good hostname with wrong password, the "rescue" block
    > will catch the error just fine. Here is the output:
    >
    > cannot connect
    > reach ensure point
    > at last
    >
    > What did I do wrong ?



    rescue without a specified exception defaults to StandardError.
    Tiimeout::Error is a subclass of SignalException, which is a sibling
    of StandardError, not a subclass. Thus that exception doesn't match
    your rescue, so the exception gets passed up the call stack, the
    ensure
    gets executed, but the code after it is bypassed.

    For a picture of the exception class hierarchy see http://
    www.insula.cz/dali/material/rubycl/RubyExceptionClasses.jpg

    cheers
    Chris
     
    ChrisH, Feb 9, 2007
    #3
  4. On Feb 9, 2:06 am, Brian Candler <> wrote:
    > On Fri, Feb 09, 2007 at 02:20:30PM +0900, S Kanakakorn wrote:
    > > require 'rubygems'
    > > require 'net/ssh'

    >
    > > begin
    > > session = Net::SSH.start('aaaa-lnx', :password=>'xyz',
    > > :username=>'root',:timeout=>5)
    > > rescue
    > > puts 'cannot connect '
    > > ensure
    > > puts 'reach ensure point'
    > > end
    > > puts 'at last'

    > ...
    > > What did I do wrong ?

    >
    > A bare "rescue" doesn't catch all exceptions, only those which are
    > subclasses of StandardError. Change to:
    >
    > rescue Exception


    Given how often this issue appears on the mailing list, I'm beginning
    to think that a bare rescue should just rescue Exception. That seems
    to be what most people expect it to do.

    Is there a downside to this idea?

    Dan
     
    Daniel Berger, Feb 9, 2007
    #4
  5. On Sat, Feb 10, 2007 at 01:45:54AM +0900, Daniel Berger wrote:
    > > A bare "rescue" doesn't catch all exceptions, only those which are
    > > subclasses of StandardError. Change to:
    > >
    > > rescue Exception

    >
    > Given how often this issue appears on the mailing list, I'm beginning
    > to think that a bare rescue should just rescue Exception. That seems
    > to be what most people expect it to do.
    >
    > Is there a downside to this idea?


    Well, it would be very confusing if a rescue intended to catch an
    invalid-data sort of error also caught an invalid program error (such as a
    "method missing" exception caused by a typo in a method name). At best,
    these would end up finding their way into a log somewhere. At worst, they
    would be silently ignored, making debugging very difficult.
     
    Brian Candler, Feb 9, 2007
    #5
    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. Richard Lionheart
    Replies:
    15
    Views:
    213
    Richard Lionheart
    Jul 18, 2004
  2. Harold Hausman

    Question about begin, rescue, end.

    Harold Hausman, Nov 18, 2005, in forum: Ruby
    Replies:
    9
    Views:
    195
    Stefan Kaes
    Nov 18, 2005
  3. Joe Ruby

    begin...rescue...retry next?

    Joe Ruby, Aug 16, 2006, in forum: Ruby
    Replies:
    8
    Views:
    225
    Joe Ruby
    Aug 16, 2006
  4. J. Cooper
    Replies:
    3
    Views:
    128
    J. Cooper
    Apr 1, 2008
  5. coderrr
    Replies:
    7
    Views:
    107
    coderrr
    May 17, 2008
Loading...

Share This Page