Sun, 15 Aug 1999 10:15:05 +0800
|
Hi Teamers
Here's one a CS2 class threw at me the other night.
--
-- procedure Invalid_Data_Loop_Test
--
-- author : rick duley
-- date : August 15, 1999
-- purpose : to demonstrate the need for a 'Skip_Line' in an exception
-- : handler to avoid the program going into an endless
-- : loop after a 'Get' call when the exception is an Ada
-- : pre-defined exception but that it doesn't seem to
-- : matter when the exception is user-defined
--
-- : try entering any character - the program will loop,
-- : <CTRL,C> will stop it - then try uncommenting the
-- : 'Skip_Line' calls and see what happens
--
-- copyright (c) rick duley 1999
--
with Ada.Text_Io;
procedure Invalid_Data_Loop_Test is
package My_Integer_Io is new Ada.Text_Io.Integer_Io(Integer);
AnInteger : Integer := 0;
Range_Error : exception;
begin -- Invalid_Data_Loop_Test
Validation_Loop:
loop
Validation_Block:
begin
Ada.Text_Io.Put(Item => "Enter an Integer" &
" between 0 and 100 " &
"(100 to QUIT) : ");
My_Integer_Io.Get(Item => AnInteger);
if AnInteger not in 0 .. 100 then
raise Range_Error;
else
exit Validation_Loop when AnInteger = 100;
end if;
Ada.Text_Io.New_Line;
Ada.Text_Io.Skip_Line;
Ada.Text_Io.Put(Item => "The number entered was : ");
My_Integer_Io.Put(Item => AnInteger, Width => 1);
Ada.Text_Io.New_Line(Spacing => 2);
exception
when Range_Error =>
-- this is where the 'Skip_Line' _is_not_ needed
-- Ada.Text_Io.Skip_Line;
Ada.Text_Io.New_Line(Spacing => 2);
Ada.Text_Io.Put_Line(Item => "You Goofed - Value Range");
Ada.Text_Io.New_Line;
when others =>
-- this is where the 'Skip_Line' _is_ needed
-- Ada.Text_Io.Skip_Line;
Ada.Text_Io.New_Line(Spacing => 2);
Ada.Text_Io.Put_Line(Item => "You Goofed");
Ada.Text_Io.New_Line;
end Validation_Block;
end loop Validation_Loop;
end Invalid_Data_Loop_Test;
Why is it that user-defined exceptions are treated in a different manner
from the pre-defined ones? What happens to the <LT> character (which I
assume to be remaining in the buffer and causing the loop) in the
Range_Error situation?
--------------------------------------------------------------------------
Rick Duley
Edith Cowan University
Perth, Western Australia
tel: +61 (08) 9370 6619
/-_|\
fax: +61 (08) 9370 6100 / \
perth *_.-._/
"The lonliest place in the world v
is the loosin' champ's dressin' room!"
(Jack Dempsey)
"He wasn't an Ada programmer in Perth!" (Rick Duley) J
|
|
|