# How to stop nested catch from looking messy?

**URL:** <https://www.ruby-forum.com/t/how-to-stop-nested-catch-from-looking-messy/83390>\
**Category:** Ruby\
**Created:** [January 25, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/how-to-stop-nested-catch-from-looking-messy/83390 "2007-01-25T18:50:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 25, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/how-to-stop-nested-catch-from-looking-messy/83390/1 "2007-01-25T18:50:29Z")

</div>

I’m writing a parser generator (for fun) and am making use of  
throw/catch to allow notification of special parse events to “bubble  
up” through many levels. The trouble is that the code starts to look  
ugly when I have to nest various catch blocks so as to catch three  
different possible types of throw.

Check out the following loop where I am iterating over a collection of  
“parseable” objects, catching and counting the different types of  
throws. The “parseable” object could be something as simple as a String  
or a complex hierarchy of parslets and parslet combinations; the main  
point here is that I don’t know what it is nor what type of throw it  
might do, I only know that it should respond to the “parse” message:

alternatives.each do |parseable|  
catch(:ProcessNextAlternative) do  
catch(:NotPredicateSuccess) do  
catch(:AndPredicateSuccess) do  
catch(:ZeroWidthParseSuccess) do  
accumulate\_result(parseable.parse)  
match\_count += 1  
throw :ProcessNextAlternative  
end  
zero\_width\_count += 1  
throw :ProcessNextAlternative  
end  
and\_predicate\_count += 1  
throw :ProcessNextAlternative  
end  
not\_predicate\_count += 1  
end  
end

Does this design stink? Is there a better/cleaner way?

Cheers,  
Greg
