stuff.ads
package Stuff is
Funny_Add_Error : exception;
procedure Add_One(Number : in out INTEGER);
function Subtract_One(Number : INTEGER) return INTEGER;
end Stuff;
stuff.adb
with Ada.Text_IO;
use Ada.Text_IO;
package body Stuff is
procedure Add_One(Number : in out INTEGER) is
begin
Number := Number + 1;
exception
when Funny_Add_Error => Put_Line("Funny add error raised");
when Constraint_Error => Put_Line("Constraint error raised");
end Add_One;
function Subtract_One(Number : INTEGER) return INTEGER is
Funny_Subtract_Error : exception;
begin
raise Funny_Subtract_Error;
return Number - 1;
exception
when Funny_Add_Error =>
Put_Line("Funny add error raised");
raise;
when Funny_Subtract_Error =>
Put_Line("Funny subtract error raised");
raise;
end Subtract_One;
begin
null;
exception
-- Numeric_Error is obsolete in Ada 95. It is another name for
-- the exception Constraint_Error.
-- when Numeric_Error =>
-- Put_Line("Numeric error during elaboration");
when Constraint_Error =>
Put_Line("Constraint_Error during elaboration");
when Funny_Add_Error =>
Put_Line("Funny Add error during elaboration");
-- when Funny_Subtract_Error =>
-- Put_Line("Funny subtract error during elaboration");
end Stuff;
main.adb
with Ada.Text_IO, Stuff;
use Ada.Text_IO, Stuff;
procedure Except5 is
Index : INTEGER := 5;
Add_Error : exception renames Stuff.Funny_Add_Error;
begin
Add_One(Index);
Index := Subtract_One(Index);
exception
when Numeric_Error | Constraint_Error =>
Put_Line("Numeric error or constraint error raised.");
when Add_Error =>
Put_Line("Addition error detected");
when others =>
Put_Line("An unknown exception raised");
raise; -- Raise it again for the operating system
end Except5;
-- Result of execution
-- Funny subtract error raised
-- An unknown exception raised
-- Unhandled exception; funny_subtract_error
-- (Note, The last line above will be different for each compiler,
-- but will say something about an unhandled exception. It
-- will probably output about 10 lines of text.)