comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey D. Cherry" <jdcherry@utech.net>
Subject: Re: Exception Handling
Date: 2000/05/30
Date: 2000-05-30T00:00:00+00:00	[thread overview]
Message-ID: <3933DAF3.35F40B3A@utech.net> (raw)
In-Reply-To: 3HCY4.29157$sB3.10828@news.indigo.ie

NANCY HEHIR wrote:
> 
> For example: If I prompt for a keyboard input of ,say an eight character
> string, and a seven char string is actually inputted, a Constraint_Error is
> raised. Can I use a handling device to return the execution to the prompt
> for input ?

Preben Randhol had a good response to this but I would like to add my 2 cents.
You could use an exception handler to trap this kind of input error but I 
prefer to use exceptions for things that are a bit more unexpected.  (Please 
note that this is a personal bias.)  Invalid input is what I would call an 
expected error, so I apply validation criteria to the input before accepting 
it.  In your example, the only criteria is that the string be 8 characters 
in length, so I would make this an explicit test.  Making an explicit test 
shows the reader of the software what I'm looking for in order to accept the
input as valid.  Depending on an exception handler to perform validation 
obscures this message.  However, to be fair, there are cases where trapping
an exception is the better method.  Clarity of the source code in expressing 
the intent should be the discriminator for whichever method is ultimately 
used.  And so, with that disclaimer, here is my suggestion ...

Following, is a function that returns an eight character string read from 
standard input.  This function assumes standard output is available for prompts 
and error messages.  I prefer using Get_Line to read strings from standard 
input but other methods may be used as well.  (A "with" and "use" of 
Ada.Text_IO is assumed.)  Note the input buffer variable should be set to some
"reasonable" length.  Its intent is to hold the largest input line entered from 
standard input.  Arbitrarily I chose a length of 512 characters.

function Get_8_Char_String return string is
   -- A lower bound of 1 for the input buffer was chosen for ease of testing 
   -- in the function body.
   Buffer : string(1..512);
   Last   : natural;
begin -- Get_8_Char_String
   loop
      Put("Please enter an 8 character string: ");
      Get_Line(Buffer, Last);
      exit when Last = 8;
      New_Line;
      Put_Line("Input error, input must be 8 characters.");
      New_Line;
   end loop;
   return Buffer(1..Last);
end Get_8_Char_String;

-- 
Regards,
Jeffrey D. Cherry
Senior IV&V Analyst
Logicon Space and Information Operations
Logicon Inc.
a Northrop Grumman company




  parent reply	other threads:[~2000-05-30  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 ` Robert Dewar
2000-05-30  0:00 ` Jeffrey D. Cherry [this message]
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     ` Ehud Lamm
2000-06-01  0:00     ` Jeffrey D. Cherry
2000-06-02  0:00       ` David C. Hoos, Sr.
2000-06-02  0:00         ` Jeffrey D. Cherry
2000-06-01  0:00     ` Preben Randhol
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
  -- strict thread matches above, loose matches on Subject: below --
1996-09-28  0:00 Robert Dewar
1996-09-17  0:00 John Goodenough
1996-09-17  0:00 Marin David Condic, 407.796.8997, M/S 731-93
1996-09-19  0:00 ` Larry Kilgallen
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 ` 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-12  0:00 ` Patrick Doyle
1996-09-12  0:00   ` Rick Decker
1996-09-13  0:00     ` Larry Kilgallen
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