comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Exception problem
Date: 1997/02/25
Date: 1997-02-25T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.97Feb25112121@spectre.mitre.org> (raw)
In-Reply-To: evans-2202972108360001@ppp1.s8.pgh.net


In article <3311FD32.2DDA@fs2.assist.uci.edu> Larry Coon <larry@fs2.assist.uci.edu> writes:

  > However, given the more powerful type definition capabilities of Ada,
  > invalid input can create exceptional conditions that wouldn't be
  > exceptional in C++.  So I guess my question is, does that make it okay
  > to use exception handling for input validation in Ada?

  There are cases where it is not only acceptable, it is the only way
to deal with bad input.  For example, if you are reading a series of
records in a specified format and encounter the end of the file in the
middle of a record it is hard to deal with the situation in any other
way.  Of course the loop that reads the records should check for end
of file after reading each record since you know that eventually you
will run out of data.

  > Or, in cases such as keyboard input, should I go on the premise
  > that the user is inputing using a keyboard, keyboards produce
  > characters, and character input is what should be expected.
  > Therefore, the correct thing to do is to accept character input,
  > validate it, and then if it's okay convert it to the appropriate
  > type.  Granted, doing it the first way is a lot easier....

   There are cases where if you don't validate the input, your program
will be erroneous.  Ada 95 added X'Valid to deal with such cases.
Whether 'Valid needs to be wrapped in an exception handler depends on
program design and the data being accessed, but in general you don't
worry about whether the 'Valid check fails or some exception is
raised:

    function Read_Next_Value return My_Type is
      Temp: My_Type;
    begin
      My_Type_IO.Get(Foo, Temp);
      if My_Type'Valid(Temp) 
      then return Temp;
      else raise Data_Error;
      end if;
    exception
      when others => raise Data_Error;
    end Read_Next_Value;

    (For those purists among you, assume that this is reading from a
data file written by some other version of the program.  Also note the
check for end of file, if needed, is assumed to be at an outer level.)

    Why map everything to Data_Error?  Why not?  During debugging you
are going to care why the data can't be read, but in most programs, if
the inital input data is bad, you can't do much to recover.  For
example you might be reading a series of entries from a configuration
file.  The error message at the next level up might specify where the
error occured:

       "Error in config file at line XXX position YY."

    But in some cases that is about all you can do.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




      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 ` Keith Allan Shillington
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 ` Joerg Rodemann
1997-02-19  0:00   ` Mats Weber
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
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 [this message]
replies disabled

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