--
-- This program reads and echos integers, but tries to print a reasonable
-- message when the data is bad. It catches the Data_Error exception to
-- do this.
--
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure ReadOut2 is
N: Integer; -- Number read.
begin
-- Issue the lovely decoration.
Put_Line("-----------------------------------------------------" &
"-----------");
-- Copy lines like there's no tomorrow. And no bad data, either.
loop
Get(N);
Put(N, 1); -- Note, the 1 strips leading spaces from output.
New_Line;
end loop;
exception
when Data_Error =>
-- This does a text read to show what the input near the error
-- looks like.
declare
S: String(1..40); -- Portion of input file.
begin
Get_Line(S, N);
Put("Bad data: '");
Put(S(1..N));
Put_Line("'");
end;
-- Note: We cannot return to the read loop at this point. This
-- procedure is over.
when End_Error =>
-- When reaching end of file, issue the closing lovely decoration and
-- return from the procedure.
Put_Line("-----------------------------------------------------" &
"-----------");
end ReadOut2;