-- Demonstrates causing and handling a storage error (caused by infinite recursion)
-- Demonstrates the exceptions package
-- Program terminates when exception occurs
-- Begin-exception-end is similar to a java try-catch
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with ada.exceptions; use ada.exceptions;
procedure excep1b is
-- Force a storage_error with infinite recursion
procedure recur is
begin -- No base case!
recur;
end recur;
n: Natural;
begin
get(n);
put_line(n'img);
recur; -- Cause infinite recursion
put_line(n'img);
exception
when data_error =>
put_line("You must enter a number!");
when constraint_error =>
put_line("You must enter an non-negative value!");
when end_error =>
put_line("Premature end of file!");
when e: others =>
put_line("Unknown exception occurred!");
put_line("An exception with this name happened: " & exception_name(e) );
put_line("The exception message with this: " & exception_message(e) );
end excep1b;
-- Input: 3
-- Output:
--| 3
--|Unknown exception occurred!
--|An exception with this name happened: STORAGE_ERROR
--|The exception message with this: excep1b.adb:12 infinite recursion