Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > HELP: Need to continue the loop after the exception

Reply
Thread Tools

HELP: Need to continue the loop after the exception

 
 
mirth
Guest
Posts: n/a
 
      02-19-2008
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.
 
Reply With Quote
 
 
 
 
mirth
Guest
Posts: n/a
 
      02-19-2008
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.
 
Reply With Quote
 
 
 
 
Christopher Swasey
Guest
Posts: n/a
 
      02-19-2008
On 2/19/08, mirth <(E-Mail Removed)> 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

 
Reply With Quote
 
mirth
Guest
Posts: n/a
 
      02-19-2008
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


 
Reply With Quote
 
Christopher Swasey
Guest
Posts: n/a
 
      02-20-2008
On 2/19/08, mirth <(E-Mail Removed)> 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

 
Reply With Quote
 
Christopher Swasey
Guest
Posts: n/a
 
      02-20-2008
On 2/19/08, mirth <(E-Mail Removed)> 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

 
Reply With Quote
 
mirth
Guest
Posts: n/a
 
      02-20-2008
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.
 
Reply With Quote
 
mirth
Guest
Posts: n/a
 
      02-20-2008
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.
 
Reply With Quote
 
Christopher Swasey
Guest
Posts: n/a
 
      02-20-2008
On 2/19/08, mirth <(E-Mail Removed)> 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

 
Reply With Quote
 
mirth
Guest
Posts: n/a
 
      02-20-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: Is there a way to continue after an exception ? Krister Svanlund Python 4 02-21-2010 02:51 AM
How to "continue" in nested do-while-loop? Jan Schmidt C Programming 14 12-18-2006 03:39 PM
Continue code run after assert exception aidy Ruby 2 09-22-2006 01:14 PM
continue out of a loop in pdb Gary Wessle Python 6 05-24-2006 03:35 AM



Advertisments