The Ada Program: occur.adb

  1 -- occur.adb:  an example of exception occurrences and exception info
  2 
  3 with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Exceptions;
  4 use Ada;
  5 
  6 procedure Occur is
  7 
  8    Zero : exception;
  9 
 10    procedure P (N: Integer) is
 11    begin
 12       if N>0 then
 13          P (N-1);    -- recursive call
 14       else
 15          raise Zero;
 16       end if;
 17    exception
 18       when E: others =>
 19          Text_IO.Put (Exceptions.Exception_Name (E));
 20          Text_IO.Put (" caught in procedure P (N=");
 21          Integer_Text_IO.Put (N, Width=>1);
 22          Text_IO.Put_Line (") and propagated.");
 23          raise;
 24    end P;
 25 
 26 begin
 27 
 28    P (5);
 29 
 30 exception
 31 
 32    when E: others =>
 33       Text_IO.Put (Exceptions.Exception_Name (E));
 34       Text_IO.Put_Line (" raised; giving up.");
 35 
 36 end Occur;