# Continue code run after assert exception

**URL:** <https://www.ruby-forum.com/t/continue-code-run-after-assert-exception/69614>\
**Category:** Ruby\
**Created:** [September 22, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/continue-code-run-after-assert-exception/69614 "2006-09-22T12:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 22, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/continue-code-run-after-assert-exception/69614/1 "2006-09-22T12:36:53Z")

</div>

I am iterating through a collection and using Test::Unit assertations  
on each object

begin  
search.each {|@x|  
assert($ie.contains\_text(@x)) }  
rescue =\> e  
puts “#{@x} does not exist in HTML”  
end

However, the problem is that if the first assertation is false, an  
exception is thrown and my loop terminates. I would like to continue  
the loop after an exception is thrown. I have tried ‘retry’, but I find  
myself in an infinite loop.

Thanks

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 22, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/continue-code-run-after-assert-exception/69614/2 "2006-09-22T12:58:56Z")

</div>

Hi –

On Fri, 22 Sep 2006, aidy wrote:

> However, the problem is that if the first assertation is false, an  
> exception is thrown and my loop terminates. I would like to continue  
> the loop after an exception is thrown. I have tried ‘retry’, but I find  
> myself in an infinite loop.

You can put the begin/rescue/end inside your loop, like this:

[1,2,3].each do |x|  
begin  
raise unless x == 2  
rescue  
puts “No! #{x}”  
end  
end

Also, you might find the asserts\_raises method useful. It lets you  
predict that something will raise an exception. It probably wouldn’t  
be a good fit with the loop structure you’re using, but you could  
isolate some exception-raising tests and test them that way.

David

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 22, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/continue-code-run-after-assert-exception/69614/3 "2006-09-22T15:15:13Z")

</div>

aidy schrieb:

> However, the problem is that if the first assertation is false, an  
> exception is thrown and my loop terminates. I would like to continue  
> the loop after an exception is thrown. I have tried ‘retry’, but I find  
> myself in an infinite loop.

Aidy, are you using this code in a unit test, or are you using the  
assertions as part of your normal code? If this is normal code, see  
David’s answer how to catch the error inside the loop.

If this is part of a unit test, and you want to report every false  
element, take a look at the code in

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/138320](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/138320)

With this module, your test could look like:

class MyTestCase \< Test::Unit::TestCase

```
 include ErrorCollector

 def test_search_screen
   ...
   collecting_errors do
     search.each do |@x|
       assert($ie.contains_text(@x), "#{@x} does not exist in HTML")
     end
   end
 end

```

end

Regards,  
Pit
