Question about begin, rescue, end.

Discussion in 'Ruby' started by Harold Hausman, Nov 18, 2005.

  1. Here's my code. It's dumb:

    require 'open-uri'
    begin
    =09while true do
    =09=09the_begin_time =3D Time.now
    =09=09open( "http://danceliquid.com/test/public/" ) do |stuff|
    =09=09=09puts stuff.read
    =09=09end
    =09=09the_wait_time =3D Time.now - the_begin_time
    =09=09the_file =3D File.new("SitePingLog.txt", "a")
    =09=09the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    Time.now.to_s)
    =09=09the_file.close
    =09=09sleep 600
    =09end
    rescue
    =09=09the_file =3D File.new("SitePingLog.txt", "a")
    =09=09the_file.puts(Error @ ' + Time.now.to_s)
    =09=09the_file.close
    =09=09retry
    end

    #What it does it hit my site every ten minutes, and keep a log of how
    long it took to get the content there. I was having people tell me
    that intermittently they wouldn't be able to get to it at all, or
    sometimes it would be really slow. This script has proven this to be
    true, and in all honesty I'm not too worried about the performance of
    my site. My question is, all the open-uri code is inside the begin,
    rescue section, and occasionally the rescue, end section gets called,
    but also sometimes (infrequently), the whole script bombs out...
    here's the error:

    c:/ruby/lib/ruby/1.8/timeout.rb:42:in `rbuf_fill': execution expired (Timeo=
    ut::E
    rror)
    from c:/ruby/lib/ruby/1.8/net/protocol.rb:196:in `timeout'
    from c:/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
    from c:/ruby/lib/ruby/1.8/net/protocol.rb:196:in `rbuf_fill'
    from c:/ruby/lib/ruby/1.8/net/protocol.rb:160:in `readuntil'
    from c:/ruby/lib/ruby/1.8/net/protocol.rb:171:in `readline'
    from c:/ruby/lib/ruby/1.8/net/http.rb:1554:in `read_status_line'
    from c:/ruby/lib/ruby/1.8/net/http.rb:1538:in `read_new'
    from c:/ruby/lib/ruby/1.8/net/http.rb:833:in `request'
    ... 9 levels...
    from c:/ruby/lib/ruby/1.8/open-uri.rb:134:in `open_uri'
    from c:/ruby/lib/ruby/1.8/open-uri.rb:424:in `open'
    from c:/ruby/lib/ruby/1.8/open-uri.rb:85:in `open'
    from C:/Documents and Settings/Harold/Desktop/scripting/ruby/SitePi=
    nger.
    rb:5

    ... Right, so, isin't begin, rescue, end supposed to stop exactly this
    from happening? Or chances are, I've misunderstood.

    Thanks in advance for your time,
    -Harold
    Harold Hausman, Nov 18, 2005
    #1
    1. Advertising

  2. On 11/17/05, Harold Hausman <> wrote:

    > ... Right, so, isin't begin, rescue, end supposed to stop exactly this
    > from happening? Or chances are, I've misunderstood.
    >
    > Thanks in advance for your time,


    I think it's the 'retry' that's causing the error.


    This is being called inside of the rescue, which in turn has no rescue.

    maybe call exit; at the end of your successful case, and then move
    retry outside of the rescue, effectively creating a loop.
    Gregory Brown, Nov 18, 2005
    #2
    1. Advertising

  3. Harold Hausman

    zdennis Guest

    Harold Hausman wrote:

    >
    > .... Right, so, isin't begin, rescue, end supposed to stop exactly this
    > from happening? Or chances are, I've misunderstood.
    >


    'rescue' by itself catches StandardError exceptions and subclasses of StandardError. The
    Timeout::Error is probably not a StandardError, but probably a subclass of Exception. You can will
    want to specfically catch Timeout::Error or Exception ....

    begin
    ...
    rescue Timeout::Error => ex # or rescue Exception => ex
    ...
    end

    HTH,

    Zach
    zdennis, Nov 18, 2005
    #3
  4. On 11/17/05, zdennis <> wrote:
    > Harold Hausman wrote:
    >
    > >
    > > .... Right, so, isin't begin, rescue, end supposed to stop exactly this
    > > from happening? Or chances are, I've misunderstood.
    > >

    >
    > 'rescue' by itself catches StandardError exceptions and subclasses of Sta=

    ndardError. The
    > Timeout::Error is probably not a StandardError, but probably a subclass o=

    f Exception.

    Yup, that's what it is:
    irb(main):004:0> Timeout::Error.ancestors
    =3D> [Timeout::Error, Interrupt, SignalException, Exception, Object, Kernel=
    ]

    Ignore my first post and catch one of the above explicitly, and that
    should solve your problem
    (excluding Object and Kernel, of course ;) )
    Gregory Brown, Nov 18, 2005
    #4
  5. Very interesting.

    I'd have guessed no arguments would catch anything, also strange how 2
    functions in the same library (open-uri) would have different
    exeception types. 500 errors return something that happens to be
    caught as a StandardError while this Timeout::Error is of a whole
    different lineage. Oh well, I'm going with it. ;) We have to operate,
    at least currently, under the assumption that the person who wrote the
    library is hell of smarter than I am.

    I trust that this will fix the problem, though I wont really ever know
    for sure because the script only crashed out in that manner every few
    days anyway.

    Thanks again for all you guys' help. Long live Ruby.

    -Harold
    p.s. Are my initial questions getting double posted? or is gmail just
    getting confused? 1000 apologies if they are getting double posted.

    On 11/17/05, Gregory Brown <> wrote:
    > On 11/17/05, zdennis <> wrote:
    > > Harold Hausman wrote:
    > >
    > > >
    > > > .... Right, so, isin't begin, rescue, end supposed to stop exactly th=

    is
    > > > from happening? Or chances are, I've misunderstood.
    > > >

    > >
    > > 'rescue' by itself catches StandardError exceptions and subclasses of S=

    tandardError. The
    > > Timeout::Error is probably not a StandardError, but probably a subclass=

    of Exception.
    >
    > Yup, that's what it is:
    > irb(main):004:0> Timeout::Error.ancestors
    > =3D> [Timeout::Error, Interrupt, SignalException, Exception, Object, Kern=

    el]
    >
    > Ignore my first post and catch one of the above explicitly, and that
    > should solve your problem
    > (excluding Object and Kernel, of course ;) )
    >
    >
    Harold Hausman, Nov 18, 2005
    #5
  6. On Nov 17, 2005, at 11:42 PM, Harold Hausman wrote:

    > Very interesting.
    >
    > I'd have guessed no arguments would catch anything,


    If you really want to catch _everything_:

    begin
    ...
    rescue Object => ex
    ...
    end

    Of course thats a little extreme.
    Logan Capaldo, Nov 18, 2005
    #6
  7. Logan Capaldo wrote:

    > If you really want to catch _everything_:
    >
    > begin
    > ...
    > rescue Object => ex


    rescue Exception => ex

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Nov 18, 2005
    #7
  8. >
    > -Harold
    > p.s. Are my initial questions getting double posted? or is gmail just
    > getting confused? 1000 apologies if they are getting double posted.
    >


    No. that's just gmail. Annoying, isn't it?
    Gregory Brown, Nov 18, 2005
    #8
  9. A general remark: You can greatly improve reliability of your code by
    replacing

    the_file = File.new("SitePingLog.txt", "a")
    the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    Time.now.to_s)
    the_file.close

    with

    File.open("SitePingLog.txt", "a") do |the_file|
    the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    Time.now.to_s)
    end

    Kind regards

    robert
    Robert Klemme, Nov 18, 2005
    #9
  10. Harold Hausman

    Stefan Kaes Guest

    Robert Klemme wrote:

    >A general remark: You can greatly improve reliability of your code by
    >replacing
    >
    >the_file = File.new("SitePingLog.txt", "a")
    >the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    >Time.now.to_s)
    >the_file.close
    >
    >with
    >
    >File.open("SitePingLog.txt", "a") do |the_file|
    > the_file.puts('Got it in '+ the_wait_time.to_s + ' seconds @ ' +
    > Time.now.to_s)
    >end
    >
    >

    I'd prefer

    File.open("SitePingLog.txt", "a") do |file|
    file.puts("Got it in #{the_wait_time} seconds @ #{Time.now}")
    end

    ;-)
    Stefan Kaes, Nov 18, 2005
    #10
    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. Kurt M. Dresner

    do...end vs. begin..end

    Kurt M. Dresner, Jul 11, 2003, in forum: Ruby
    Replies:
    3
    Views:
    103
  2. Richard Lionheart
    Replies:
    15
    Views:
    180
    Richard Lionheart
    Jul 18, 2004
  3. Joe Ruby

    begin...rescue...retry next?

    Joe Ruby, Aug 16, 2006, in forum: Ruby
    Replies:
    8
    Views:
    201
    Joe Ruby
    Aug 16, 2006
  4. Iñaki Baz Castillo

    How to use "retry" into two deep begin/rescue/end ?

    Iñaki Baz Castillo, Apr 3, 2008, in forum: Ruby
    Replies:
    4
    Views:
    86
    Iñaki Baz Castillo
    Apr 3, 2008
  5. RichardOnRails
    Replies:
    4
    Views:
    169
    RichardOnRails
    Sep 27, 2010
Loading...

Share This Page