# Trapping errors, a simple example

**URL:** <https://www.ruby-forum.com/t/trapping-errors-a-simple-example/56246>\
**Category:** Ruby\
**Created:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/trapping-errors-a-simple-example/56246 "2006-01-05T17:44:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/trapping-errors-a-simple-example/56246/1 "2006-01-05T17:44:38Z")

</div>

Hey Chaps,

Here’s a really simple menu with only 4 options. I’d like to be able do  
some simple error trapping… How’s it done?

#!/usr/bin/ruby  
#Wed Jan 4 06:17:22 GMT 2006  
class TopMenu  
def welcome  
puts “Welcome to de-blah-de-blah-de-blah”  
puts “ ~~~~~~~~~  ~~~~~~  ~~~~~~  ~~~~~~~ ”  
end

def options  
# we will use this data to create a class called Option  
# each option will have a new menu  
# and will be a child class of the main one  
print “------------------------------------\nmenu - Which stage are  
you at?\n------------------------------------\n0:- Before site work\n1:-  
During site work\n2:- After site work\n3:- Other stuff\nq:- quit!\n”  
end

# grab the input

def topchoice  
print "enter your choice (0,1,2,q) : "  
p = $stdin.gets # TODO:- check it’s validity # TODO:- is the choice  
a number in the correct range? # TODO:- does he wanna quit?  
print “you have chosen #{p}”  
if p == “0”  
print “you have chosen #{p}”  
else  
if p == “q”  
print “you have chosen #{p}”  
end  
end  
end

# create a new instance

top = TopMenu.new  
top.welcome  
top.options  
top.topchoice

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 5, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/trapping-errors-a-simple-example/56246/2 "2006-01-05T17:47:52Z")

</div>

John M. wrote:

> ```
> puts " ~~~~~~~~~  ~~~~~~  ~~~~~~  ~~~~~~~"
> 
> ```
> 
> # grab the input
> 
> def topchoice  
> print "enter your choice (0,1,2,q) : "  
> p = $stdin.gets # TODO:- check it’s validity # TODO:- is the

p.chomp!

if /\a[012q]\Z/

# ok

case p  
when 0  
when 1  
…  
end  
else

# error

end

HTH

```
robert
```

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/trapping-errors-a-simple-example/56246/3 "2006-01-05T19:57:28Z")

</div>

On Jan 5, 2006, at 10:37 AM, Robert K. wrote:

> > ```
> > puts "Welcome to de-blah-de-blah-de-blah"
> > 
> > ```
> > 
> > quit!\n" end  
> > case p  
> > when 0  
> > when 1  
> > …  
> > end  
> > else
> 
> # error
> 
> end

You might also want to take a look at HighLine[1], which can make  
this kind of user interaction easier.

1: [http://rubyforge.org/projects/highline/](http://rubyforge.org/projects/highline/)

James Edward G. II
