The Ada Program: madness.adb

  1 -- From Barnes, page 141
  2 --The following sequence of events happen.
  3 --* Procedure P.F is called.
  4 --* Procedure  G is called.
  5 --* Procedure P.H is called.
  6 --* The exception E is raised by P.H.
  7 --* P.H has no handler.
  8 --* Procedure G catchs the exception with others clause.
  9 --* Procedure G reraises E anonymously.
 10 --* Procedure F catchs the exception and prints "Got it!".
 11 
 12 with Ada.Text_IO, Ada.Integer_Text_IO;
 13 use Ada;
 14 
 15 procedure Madness is
 16 
 17    package P is         --  specification of package P
 18       procedure F;
 19       procedure H;
 20    end P;
 21 
 22    procedure G is
 23    begin
 24       P.H;
 25    exception
 26       when others => raise;
 27    end G;
 28 
 29    package body P is    --  implementation of package P
 30       E : exception;    --  scope of E is only P
 31       procedure F is
 32       begin
 33          G;
 34       exception
 35          when E => Text_IO.Put ("Got it!");
 36       end F;
 37       procedure H is     --  proc H just raises E
 38       begin
 39          raise E;
 40       end H;
 41    end P;
 42 
 43 begin
 44    P.F;
 45 end Madness;