1

When executing my user-created get procedure, in the exception block, my program handles my user-defined exceptions fine but goes into an infinite loop when a data exception occurs.

PROCEDURE Get (File: IN Ada.Text_IO.File_Type; Item : OUT Rational) IS

N: Integer;
D: Integer;
Dummy: Character;  -- dummy character to hold the "/"

BEGIN -- Get
LOOP
BEGIN
  Ada.Integer_Text_IO.Get(File => File, Item => N);
  Ada.Text_IO.Get  (File => File, Item => Dummy);
  Ada.Integer_Text_IO.Get(File => File, Item => D);
  Item := N/D;
  if Dummy /= '/' then
    raise BadFormat;
  end if;

EXIT;

EXCEPTION
 when ZeroDenominator =>
   Ada.Text_IO.Put_Line("No Zeroes in Denominator, Please enter data again.");
 when BadFormat =>
   Ada.Text_IO.Put_Line("Incorrect Formatting, Correct Format is   Numerator/Denominator.");
 when ADA.IO_EXCEPTIONS.DATA_ERROR =>
   Ada.Text_IO.Put_Line("Data Error Occurred, re-enter data like this: ");
   Ada.Text_IO.Put_Line("Numerator/Denominator");
END;
END LOOP;
END Get;

This is the output I get:

Data Error Occurred, re-enter data like this: 
Numerator/Denominator
Data Error Occurred, re-enter data like this: 
Numerator/Denominator
Data Error Occurred, re-enter data like this: 
Numerator/Denominator
Data Error Occurred, re-enter data like this: 
Numerator/Denominator

(infinitely repeats)

Does anyone have any advice on this error? thanks

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Tim
  • 11
  • 1
  • 1
    There's probably a newline in the input that is never read. Try adding `Ada.Text_IO.Skip_Line(File);` – egilhh Oct 14 '15 at 06:19
  • 2
    Looks like you're not flushing the input in the exception handler as Egil says, thus the same text raises the same exception again. –  Oct 14 '15 at 10:17
  • Ok, thank you very much, the skip_line in the exception handler solved my problem, so was the issue that the input stayed the same so the same exception was raised as soon as the code looped back, never giving it a chance to read more data? just wondering – Tim Oct 15 '15 at 16:25
  • 3
    You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod Oct 15 '15 at 20:47
  • It would be pretty easy for me to look at the RM's I/O package definition and figure out exactly what happened that caused it to loop infinitely without consuming any input, if only I knew what the input looked like. – ajb Oct 16 '15 at 05:20

0 Answers0