HELP: Need to continue the loop after the exception

Discussion in 'Ruby' started by mirth, Feb 19, 2008.

  1. mirth

    mirth Guest

    hi guys,

    I'm a ruby newbie. I have a question about how to continuethe loop
    after the exception. If anyone here could help me, I really appreciate
    that.

    example:

    I have two methods:

    def looptest
    "do sth"
    rescue
    "do sth"
    end

    def test_01
    somearray.each{|obj|
    looptest
    }
    end


    Right now if there's an exception in looptest, it will exit the loop.
    But I want the loop in test_01 to continue the next one even if
    there's an exception for the looptest in the previous loop. Is there
    anyway to achieve this? I tried "continue", "return","break" and etc.
    But nothing works so far. :(
    mirth, Feb 19, 2008
    #1
    1. Advertising

  2. mirth

    mirth Guest

    Another question:

    When the exception happens, the browser is still in the process of
    rendering page. Even the exception happens, the browser will keep
    rending the page until it's done. Is there anyway to stop the browser
    when the exception happens?

    Thanks again.
    mirth, Feb 19, 2008
    #2
    1. Advertising

  3. On 2/19/08, mirth <> wrote:
    > hi guys,
    >
    > I'm a ruby newbie. I have a question about how to continuethe loop
    > after the exception. If anyone here could help me, I really appreciate
    > that.
    >
    >
    > Right now if there's an exception in looptest, it will exit the loop.
    > But I want the loop in test_01 to continue the next one even if
    > there's an exception for the looptest in the previous loop. Is there
    > anyway to achieve this? I tried "continue", "return","break" and etc.
    > But nothing works so far. :(


    I can't replicate the behaviour you describe. Slightly modifying your
    code to test:


    def looptest
    puts "#"
    raise "Exception"
    rescue
    puts "E"
    end

    def test_01
    [1,2,3,4,5].each do |obj|
    looptest
    end
    end

    test_01

    Simply capturing the exception with a rescue should work fine. This
    example, for instance, spits out '#\nE' 5 times.

    Christopher
    Christopher Swasey, Feb 19, 2008
    #3
  4. mirth

    mirth Guest

    Is that because Watir Unit Test is behaving differently Or is that
    because the browser is still running the page even the exception
    happens?



    > I can't replicate the behaviour you describe. Slightly modifying your
    > code to test:
    >
    > def looptest
    >   puts "#"
    >   raise "Exception"
    > rescue
    >   puts "E"
    > end
    >
    > def test_01
    >   [1,2,3,4,5].each do |obj|
    >     looptest
    >   end
    > end
    >
    > test_01
    >
    > Simply capturing the exception with a rescue should work fine. This
    > example, for instance, spits out '#\nE' 5 times.
    >
    > Christopher
    mirth, Feb 19, 2008
    #4
  5. On 2/19/08, mirth <> wrote:
    > Is that because Watir Unit Test is behaving differently Or is that
    > because the browser is still running the page even the exception
    > happens?
    >
    >


    Could you be more specific about the exception that is being thrown
    and the code that's throwing it? It could be that the exception being
    thrown isn't a StandardError. NoMemoryError, for instance, wouldn't
    rescue cleanly. On a hunch, is the code particularly long running
    (more than 30 seconds), and on a shared web-host?

    Christopher



    Christopher
    Christopher Swasey, Feb 20, 2008
    #5
  6. On 2/19/08, mirth <> wrote:
    > Is that because Watir Unit Test is behaving differently Or is that
    > because the browser is still running the page even the exception
    > happens?
    >


    I forgot to mention: IIRC, "rescue" only rescues StandardError by
    default. If you want to rescue NoMemoryError or SystemExit, you have
    to either rescue those specifically, or use "rescue Exception" to
    capture *everything*. Then again, if you run into either of those
    exceptions you probably have bigger problems with your code.

    Christopher
    Christopher Swasey, Feb 20, 2008
    #6
  7. mirth

    mirth Guest

    Hi Christopher,

    Here's more specific about what I do


    In the watir unit test class I have:


    def looptest
    "do sth"
    waiter = Waiter.new(300) # 5 min
    waiter.wait_until {
    if ($browser.frame("mainFrame").div:)text, /
    Reports/).exists?)
    true
    end
    rescue
    "do sth"
    end


    def test_01
    somearray.each{|obj|
    looptest
    }
    end


    The TimeOutException will happen when the browser doesn't return
    Reports page in 5 minutes. When this exception happens, the browser
    will keep running but test will exit the loop. But I need test_01 to
    continue the loop even there's an exception and I want browser to
    stop
    when there's an exception.
    mirth, Feb 20, 2008
    #7
  8. mirth

    mirth Guest

    Hi Christopher,

    Here's the more specific example of what I'm trying to do:


    In the watir unit test class I have:


    def looptest
    "do sth"
    waiter = Waiter.new(300) # 5 min
    waiter.wait_until {
    if ($browser.frame("mainFrame").div:)text, /
    Reports/).exists?)
    true
    end
    rescue
    "do sth"
    end


    def test_01
    somearray.each{|obj|
    looptest
    }
    end


    The TimeOutException will happen when the browser doesn't return
    Reports page in 5 minutes. When this exception happens, the browser
    will keep running but test will exit the loop. But I need test_01 to
    continue the loop even there's an exception and I want browser to
    stop
    when there's an exception.
    mirth, Feb 20, 2008
    #8
  9. On 2/19/08, mirth <> wrote:
    > In the watir unit test class I have:
    >
    >
    > def looptest
    > "do sth"
    > waiter = Waiter.new(300) # 5 min
    > waiter.wait_until {
    > if ($browser.frame("mainFrame").div:)text, /
    > Reports/).exists?)
    > true
    > end
    > rescue
    > "do sth"
    > end
    >
    >
    > def test_01
    > somearray.each{|obj|
    > looptest
    > }
    > end
    >
    >
    > The TimeOutException will happen when the browser doesn't return
    > Reports page in 5 minutes. When this exception happens, the browser
    > will keep running but test will exit the loop. But I need test_01 to
    > continue the loop even there's an exception and I want browser to
    > stop
    > when there's an exception.


    Gosh. I'm stumped. I've been playing around with as close a facsimile
    of your code environment as possible without actually using watir to
    connect to your web app. I just can't replicate your behaviour. The
    only thing that jumps out at me is that you're missing a closing } on
    your wait_until block, but that should throw a syntax error if it was
    more than a typo here. There's no reason the
    Watir::Exception::TimeOutException shouldn't be caught, from what I
    can tell. (and that is what's happening, from your description: the
    exception isn't being caught, just as surely if you didn't have that
    rescue in there)

    TimeOutException ultimately descends from StandardError, so it should
    be caught just fine, but one thing you can try is:

    rescue Watir::Exception::TimeOutException

    If all else fails, you can put the rescue within the iterator block:

    begin
    looptest
    rescue Watir::Exception::TimeOutException
    end

    And that should catch the exception, no matter what. If there's more
    to your code than what you've pasted (and since your code as currently
    written would throw a syntax error, I have a hunch there is) then I'd
    really drill down on it, because something is amiss.

    Christopher
    Christopher Swasey, Feb 20, 2008
    #9
  10. mirth

    mirth Guest

    Sorry I forgot to put closing } in the example code. But it's there in
    my original code. I didn't make myself clear. The test is supposed to
    run multiple reports. During running one long report, after 5 minutes
    (the wait time I put in), the TimeOutException happens and is caught
    with rescue. After the exception is caught, the loop stops (that means
    all other reports after that won't be run) but the browser is still
    rendering the current report until it's done. What I want is that the
    browser will stop rending the current report after the exception is
    caught but continue to run the the rest reports. Thanks again for your
    help.
    mirth, Feb 20, 2008
    #10
  11. On 2/19/08, mirth <> wrote:
    > Sorry I forgot to put closing } in the example code. But it's there in
    > my original code. I didn't make myself clear. The test is supposed to
    > run multiple reports. During running one long report, after 5 minutes
    > (the wait time I put in), the TimeOutException happens and is caught
    > with rescue. After the exception is caught, the loop stops (that means
    > all other reports after that won't be run) but the browser is still
    > rendering the current report until it's done. What I want is that the
    > browser will stop rending the current report after the exception is
    > caught but continue to run the the rest reports. Thanks again for your
    > help.


    Based on your code, and my own tests with Waiter and Watir::TestCase,
    that behaviour shouldn't occur. I'm kind of at a loss.

    One more question: When you run the test, what is the result? Does it
    get counted as an error?

    Christopher
    Christopher Swasey, Feb 20, 2008
    #11
  12. mirth

    mirth Guest

    Sorry Christopher. I found the problem. I was so dumb that I forgot I
    changed my sql statement to select top 1 instead of all. The result of
    select was put into an array to drive the loop. If I only have one
    result coming back, surely the loop should only be once. Now I changed
    back to select all, and the loop was working fine. All I can say I'm
    sorry to waste your time.
    mirth, Feb 20, 2008
    #12
    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. Gary Wessle

    continue out of a loop in pdb

    Gary Wessle, May 14, 2006, in forum: Python
    Replies:
    6
    Views:
    599
    R. Bernstein
    May 24, 2006
  2. Jan Schmidt

    How to "continue" in nested do-while-loop?

    Jan Schmidt, Dec 15, 2006, in forum: C Programming
    Replies:
    14
    Views:
    985
    Richard Heathfield
    Dec 18, 2006
  3. Krister Svanlund
    Replies:
    4
    Views:
    678
    Ryan Kelly
    Feb 21, 2010
  4. aidy
    Replies:
    2
    Views:
    187
    Pit Capitain
    Sep 22, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    348
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page