[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

HELP: Need to continue the loop after the exception

mirthcyy

2/19/2008 11:24:00 PM

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. :(
11 Answers

mirthcyy

2/19/2008 11:35:00 PM

0

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.

Christopher Swasey

2/19/2008 11:44:00 PM

0

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. :(

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

mirthcyy

2/19/2008 11:53:00 PM

0

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

Christopher Swasey

2/20/2008 12:10:00 AM

0

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?
>
>

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

2/20/2008 12:15:00 AM

0

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

mirthcyy

2/20/2008 12:33:00 AM

0

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.

mirthcyy

2/20/2008 12:34:00 AM

0

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.

Christopher Swasey

2/20/2008 1:36:00 AM

0

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.

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

mirthcyy

2/20/2008 1:51:00 AM

0

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.

Christopher Swasey

2/20/2008 2:16:00 AM

0

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.

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