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/05/30 Message-ID: <3933DAF3.35F40B3A@utech.net>#1/1 X-Deja-AN: 629046902 Content-Transfer-Encoding: 7bit References: <3HCY4.29157$sB3.10828@news.indigo.ie> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii X-Trace: azure.impulse.net 959699850 192 207.154.66.125 Organization: Logicon MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-05-30T00:00:00+00:00 List-Id: 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