From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,eee47022b0e39dbb X-Google-Attributes: gid103376,public From: do_while@lo-pan.ridgecrest.ca.us (Do-While Jones) Subject: Re: Exception problem Date: 1997/02/25 Message-ID: <5eutjo$rlg@ash.ridgecrest.ca.us>#1/1 X-Deja-AN: 221324201 References: <330A0D25.313@fs2.assist.uci.edu> <3311BFC4.7576@fs2.assist.uci.edu> Organization: RidgeNet - SLIP/PPP Internet, Ridgecrest, CA. (619) 371-3501 Newsgroups: comp.lang.ada Date: 1997-02-25T00:00:00+00:00 List-Id: 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 | +--------------------------------+