From: "Jeffrey D. Cherry" <jdcherry@utech.net>
Subject: Re: Exception Handling
Date: 2000/06/01
Date: 2000-06-01T00:00:00+00:00 [thread overview]
Message-ID: <393677C0.E1A2457F@utech.net> (raw)
In-Reply-To: slrn8jccst.232.randhol+nospam@kiuk0156.chembio.ntnu.no
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
next prev parent reply other threads:[~2000-06-01 0:00 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-05-29 0:00 Exception Handling NANCY HEHIR
2000-05-29 0:00 ` Preben Randhol
2000-05-30 0:00 ` Jeffrey D. Cherry
2000-05-30 0:00 ` Gautier
2000-06-05 0:00 ` Robert Dewar
2000-06-01 0:00 ` Preben Randhol
2000-06-01 0:00 ` Preben Randhol
2000-06-01 0:00 ` Ehud Lamm
2000-06-01 0:00 ` Jeffrey D. Cherry [this message]
2000-06-02 0:00 ` David C. Hoos, Sr.
2000-06-02 0:00 ` Jeffrey D. Cherry
2000-06-01 0:00 ` Jeff Carter
2000-06-02 0:00 ` Jeffrey D. Cherry
2000-06-02 0:00 ` Preben Randhol
2000-05-30 0:00 ` Antonio Dur�n Dom�nguez
2000-05-30 0:00 ` Robert Dewar
-- strict thread matches above, loose matches on Subject: below --
1996-09-28 0:00 Robert Dewar
1996-09-17 0:00 Marin David Condic, 407.796.8997, M/S 731-93
1996-09-19 0:00 ` Larry Kilgallen
1996-09-17 0:00 John Goodenough
1996-09-13 0:00 Marin David Condic, 407.796.8997, M/S 731-93
1996-09-15 0:00 ` Larry Kilgallen
1996-09-23 0:00 ` Robin Vowels
1996-09-12 0:00 Robbie Gates
1996-09-12 0:00 ` Patrick Doyle
1996-09-12 0:00 ` Rick Decker
1996-09-13 0:00 ` Larry Kilgallen
1996-09-12 0:00 ` Bryce
1996-09-12 0:00 ` Larry Kilgallen
1996-09-13 0:00 ` Robbie Gates
1996-09-14 0:00 ` Paul A. Houle
1996-09-18 0:00 ` Rick Decker
1996-09-13 0:00 ` Felix Kasza
1996-09-13 0:00 ` David B. Shapcott [C]
1996-09-18 0:00 ` Bart Termorshuizen
1996-09-16 0:00 ` Norman H. Cohen
1996-09-23 0:00 ` Robin Vowels
1996-09-24 0:00 ` Bob Halpern
1996-10-02 0:00 ` Fritz Schneider
1996-10-07 0:00 ` Robin Vowels
1996-10-09 0:00 ` shmuel
1996-10-09 0:00 ` Bob Halpern
1996-09-26 0:00 ` Thiago
1986-05-13 22:57 exception handling MIXSIM
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox