# Continue code run after assert exception

**URL:** <https://rubytalk.org/t/continue-code-run-after-assert-exception/31223>\
**Category:** ruby-talk\
**Created:** [22 September 2006 10:35 UTC](https://rubytalk.org/t/continue-code-run-after-assert-exception/31223 "2006-09-22T10:35:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy2](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aidy2](https://rubytalk.org/u/aidy2)\
**Post date:** [22 September 2006 10:35 UTC](https://rubytalk.org/t/continue-code-run-after-assert-exception/31223/1 "2006-09-22T10:35:07Z")

</div>

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

&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search.each {|@x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert($ie.contains\_text(@x)) }  
&nbsp;&nbsp;&nbsp;rescue =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{@x} does not exist in HTML"  
&nbsp;&nbsp;&nbsp;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.  
&nbsp;&nbsp;&nbsp;  
Thanks  
&nbsp;&nbsp;&nbsp;  
aidy

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [22 September 2006 10:57 UTC](https://rubytalk.org/t/continue-code-run-after-assert-exception/31223/2 "2006-09-22T10:57:02Z")

</div>

Hi --

> I am iterating through a collection and using Test::Unit assertations  
> on each object
> 
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;search.each {|@x|  
> &nbsp;&nbsp;&nbsp;&nbsp;assert($ie.contains\_text(@x)) }  
> &nbsp;&nbsp;rescue =\> e  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "#{@x} does not exist in HTML"  
> &nbsp;&nbsp;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.

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

[1,2,3].each do |x|  
&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise unless x == 2  
&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "No! #{x}"  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Fri, 22 Sep 2006, aidy wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [22 September 2006 13:14 UTC](https://rubytalk.org/t/continue-code-run-after-assert-exception/31223/3 "2006-09-22T13:14:30Z")

</div>

aidy schrieb:

> I am iterating through a collection and using Test::Unit assertations  
> on each object
> 
> &nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search.each {|@x|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert($ie.contains\_text(@x)) }  
> &nbsp;&nbsp;&nbsp;rescue =\> e  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{@x} does not exist in HTML"  
> &nbsp;&nbsp;&nbsp;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.

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

&nbsp;&nbsp;&nbsp;[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:

&nbsp;&nbsp;&nbsp;class MyTestCase \< Test::Unit::TestCase

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include ErrorCollector

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_search\_screen  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collecting\_errors do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search.each do |@x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert($ie.contains\_text(@x), "#{@x} does not exist in HTML")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;end

Regards,  
Pit
