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: "Jeffrey D. Cherry" Subject: Re: Exception Handling Date: 2000/06/01 Message-ID: <393677C0.E1A2457F@utech.net>#1/1 X-Deja-AN: 629894791 Content-Transfer-Encoding: 7bit References: <3HCY4.29157$sB3.10828@news.indigo.ie> <3933DAF3.35F40B3A@utech.net> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii X-Trace: azure.impulse.net 959871072 192 207.154.66.108 Organization: Logicon MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-06-01T00:00:00+00:00 List-Id: 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