-- File: ExceptionDemo.adb -- Smorgasbord: Includes raising, catching, and declaring exceptions with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; procedure ExceptionDemo is -- Demo constraint error. -- For Exceptions in the language: -- http://archive.adaic.com/standards/83lrm/html/lrm-11-01.html procedure Divide_By_Zero(Count : Integer) is Divide_Result : Integer; begin Put("Count is"); Put(Count, 3); Put(" and the answer is"); Divide_Result := 25 / (Count - 4); Put(Divide_Result, 4); New_Line; exception when Constraint_Error => Put_Line(" Divide by zero occurred"); end Divide_By_Zero; -- Demo creating and raising an exception. procedure Raise_An_Error(Count : Integer) is My_Own_Error : exception; -- Declare it Another_Result : Integer; begin Put("Count is"); Put(Count, 3); Another_Result := 35 / (Count - 6); -- untested divide by zero if Count = 3 then raise My_Own_Error; -- Raise it end if; Put_Line(" and is a legal value"); exception -- Catch it when My_Own_Error => Put_Line(" My own error occurred"); end Raise_An_Error; begin for Count in 1..7 loop Divide_By_Zero(Count); Raise_An_Error(Count); -- Will crash program when Count is 6 end loop; Put_Line("End of program."); exception when Constraint_Error => Put(" Constraint error detected at"); Put_Line(" the main program level."); Put_Line("Program terminated."); end ExceptionDemo;