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.
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?
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
···
On 2/19/08, mirth <mirthcyy@gmail.com> 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.
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
···
On 2/19/08, mirth <mirthcyy@gmail.com> 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
···
On 2/19/08, mirth <mirthcyy@gmail.com> 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?
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.
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
···
On 2/19/08, mirth <mirthcyy@gmail.com> 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.
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
···
On 2/19/08, mirth <mirthcyy@gmail.com> 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.
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.