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,436ac666600e5ab3 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Exception Handling Date: 2000/06/02 Message-ID: #1/1 X-Deja-AN: 630168796 Content-Transfer-Encoding: 7bit References: <3HCY4.29157$sB3.10828@news.indigo.ie> <3933DAF3.35F40B3A@utech.net> <393677C0.E1A2457F@utech.net> Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 959940550 158.252.123.169 (Fri, 02 Jun 2000 03:09:10 PDT) Organization: Ada95 Press, Inc. MIME-Version: 1.0 NNTP-Posting-Date: Fri, 02 Jun 2000 03:09:10 PDT Newsgroups: comp.lang.ada Date: 2000-06-02T00:00:00+00:00 List-Id: I'm curious as to why you wouldn't simply use the 'Value attribute of the specific type to attempt the conversion. This avoids the expense of an instance of Text_IO.Float_IO which might be needed for no other purpose, and makes use of the compiler vendor's parsing routine. If an exception were raised, the offending string could be displayed -- e.g: The_Value : Some_Float_Type; The_Input_String : String (1 .. 80) Last : Natural . . . loop Ada.Text_IO.Get_Line (The_Input_String, Last); begin The_Value := Some_Float_Type'Value (The_String (1 .. Last); exit; -- Will exit if conversion succeeds exception when E: others => Ada.Text_IO.Put ("Invalid input """ & The_Input_String (1 .. Last) & """; try again."); end; end loop; -- Use The_Value Jeffrey D. Cherry wrote in message news:393677C0.E1A2457F@utech.net... > Preben Randhol wrote: > > > > Would it be OK to trap an erronious entry (like "2.3W-4") with an > > exception or would you make an extra check before trying to convert the > > string to a float (or the appropriate class). > > > > This, in my opinion, is one of those cases where it is better to trap an > exception than code a specific routine to parse floating point numbers. > However, I would use something like Ada.Float_Text_IO (for the standard float > type) or an appropriate instance of Ada.Text_IO.Float_IO to attempt the > conversion using: > procedure Get(From : in String; Item : out Num; Last : out Positive); > In this case I would read the input string, extract the delimited string that > should contain the floating point number, and then use that slice in the > call to Get. To trap the exception, I would use either a block statement or > create a special subprogram like the following: > > procedure Get_Float(St : in string; Value : out float; Is_Valid : out boolean) > is > Last : positive; > begin -- Get_Float > Ada.Float_Text_IO.Get(St, Value, Last); > Is_Valid := true; > exception > when others => > Is_Valid := false; > end Get_Float; > > The drawback here is that I have no specific information regarding what was > wrong with the input. The good thing is that I don't have to write a lengthy > subprogram to parse the floating point number and this provides the readability > that I'm seeking. It also uses the exception Data_Error if there is something > wrong with the input format or the input could not be converted to the > type float. This avoids the problem of a suppressed constraint check, which > some compilers use as the default. > > There are situations where I would want to write a validation routine that > does not depend on the standard library I/O packages. One obvious example > would be an application targeted for an embedded computer. In these cases, > the Ada run-time library (such as Text_IO and its children) are very limited > or not available. In this case I would write my own validation subprogram > to parse the input string; probably something like a small finite state > machine. Of course, I'm moving off our original example here since we assumed > that I/O was available through standard input and standard output, and we're > validating input from a user-input string. But you get the idea of the > situations where I would and would not use an exception to validate input. > > -- > Regards, > Jeffrey D. Cherry > Senior IV&V Analyst > Logicon Space and Information Operations > Logicon Inc. > a Northrop Grumman company >