# Nice pattern for running a series of error checks

**URL:** <https://www.ruby-forum.com/t/nice-pattern-for-running-a-series-of-error-checks/129938>\
**Category:** Ruby\
**Created:** [February 22, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/nice-pattern-for-running-a-series-of-error-checks/129938 "2008-02-22T21:43:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 22, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/nice-pattern-for-running-a-series-of-error-checks/129938/1 "2008-02-22T21:43:24Z")

</div>

came up with this while refactoring some code:

case  
when test\_1; report\_error test\_1  
when test\_2; report\_error test\_2  
when test\_3; report\_error test\_3  
else; do\_success

the nice thing is that it automatically exits the structure at the  
first failing test.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 25, 2008, 10:47am UTC](https://www.ruby-forum.com/t/nice-pattern-for-running-a-series-of-error-checks/129938/2 "2008-02-25T10:47:44Z")

</div>

On Sat, Feb 23, 2008 at 05:42:28AM +0900, Martin DeMello wrote:

> case  
> when test\_1; report\_error test\_1  
> when test\_2; report\_error test\_2  
> when test\_3; report\_error test\_3  
> else; do\_success
> 
> the nice thing is that it automatically exits the structure at the  
> first failing test.

Well. The preferred way to report error should be raising exceptions, in  
which case everything up to the nearest error handler is exited at the  
first failing test…

Sylvain
