comp.lang.ada
 help / color / mirror / Atom feed
From: do_while@lo-pan.ridgecrest.ca.us (Do-While Jones)
Subject: Re: Exception problem
Date: 1997/02/25
Date: 1997-02-25T00:00:00+00:00	[thread overview]
Message-ID: <5eutjo$rlg@ash.ridgecrest.ca.us> (raw)
In-Reply-To: 3311BFC4.7576@fs2.assist.uci.edu


In article <3311BFC4.7576@fs2.assist.uci.edu> larry@fs2.assist.uci.edu writes:
>Arthur Evans Jr wrote:
>
>> . . . Add a call to Skip_Line.
>
>That did the trick, thanks.
>
>By the way, that brings up another question....that of when it's
>appropriate to use exceptions.  In C++, for example, you wouldn't want
>to throw an exception because your number should be positive and you got
>a negative.  In C++, you're using an int to represent this number, and
>for an int to be -3 is not an exceptional condition.  However, in Ada
>subtypes and derived types allow you to declare integers for which -3
>truly is an exceptional condition, making it very appropriate to raise
>(see, I even got the terminology right!) an exception.
>
>Writing that code almost made me feel guilty for using exceptions the
>wrong way, and I wanted to make sure I'm thinking about them in the
>right way in Ada.

I would have written the program almost exactly the way you did.  The 
only difference is that I would have used "Get_Line(TEXT, LENGTH);" 
instead of Get.  That would have eliminated the need for the Skip_Line.

After obtaining the text from the user, I would have used the 'VALUE 
attribute to convert TEXT(1 .. LENGTH) to the integer type, if possible.  
The 'VALUE attribute will raise an exception if the text string isn't an 
integer, so you would have no choice but to use an exception handler.

If the 'VALUE attribute raises CONSTRAINT_ERROR when trying to convert 
the string to an integer, I normally send an error message that shows the 
incorrect input.  For example,

Put_Line(TEXT(1 .. LENGTH) & " is not an integer."); 

Then I loop back and ask for the input again, as you did.

Using Get_Line to get the entire text string also gives me the opportunity
to make a remarkably smart user interface.  Suppose some smart-aleck user
enters "XXV" or "twenty-five".  I could write an exception handler that
tries to interpret the string when the 'VALUE attribute fails.  The
handler could be written to recognize Roman numerals and number names.  If
it successfully interprets the value it could respond,

Put_Line("Why didn't you just enter" & integer'IMAGE(I) & "?");
Put_Line("Don't answer that.  I'm just thinking out loud.")
Put_Line("I will now process your data.");

(Management would probably not allow me to leave that in the final 
product, but sometimes I can sneak those things through.)

>Larry Coon
>University of California
>larry@fs2.assist.uci.edu

Do-While Jones




-- 
            +--------------------------------+
            |    Know                 Ada    |
            |        [Ada's Portrait]        |
            |    Will              Travel    |
            | wire do_while@ridgecrest.ca.us |
            +--------------------------------+




  parent reply	other threads:[~1997-02-25  0:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-02-18  0:00 Exception problem Larry Coon
1997-02-18  0:00 ` Michael Feldman
1997-02-19  0:00   ` Larry Coon
1997-02-19  0:00 ` Joerg Rodemann
1997-02-19  0:00   ` Mats Weber
1997-02-19  0:00 ` David C. Hoos, Sr.
1997-02-19  0:00   ` Robert Dewar
1997-02-19  0:00   ` Larry Coon
1997-02-19  0:00 ` Keith Allan Shillington
1997-02-22  0:00 ` Arthur Evans Jr
1997-02-24  0:00   ` Larry Coon
1997-02-24  0:00     ` Larry Kilgallen
1997-02-24  0:00       ` Larry Coon
1997-02-25  0:00         ` Fergus Henderson
1997-02-25  0:00     ` Do-While Jones [this message]
1997-03-09  0:00       ` John Volan
1997-03-09  0:00         ` Robert Dewar
1997-03-12  0:00         ` Keith Thompson
1997-02-25  0:00   ` Robert I. Eachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox