Show how to create a user-defined exception   and   show how to catch an exception raised from several nested calls away.

   Create two user-defined exceptions,   U0   and   U1. 
   Have function   foo   call function   bar   twice.
   Have function   bar   call function   baz. 
   Arrange for function   baz   to raise, or throw exception   U0   on its first call, then exception   U1   on its second.
   Function   foo   should catch only exception   U0,   not   U1.


Show/describe what happens when the program is run.




with Ada.Text_Io; use Ada.Text_Io;
 
procedure Exceptions_From_Nested_Calls is
   U0 : exception;
   U1 : exception;
   Baz_Count : Natural := 0;
   procedure Baz is
   begin
      Baz_Count := Baz_Count + 1;
      if Baz_Count = 1 then
         raise U0;
      else
         raise U1;
      end if;
   end Baz;
   procedure Bar is
   begin
      Baz;
   end Bar;
   procedure Foo is
   begin
      Bar;
   exception
      when U0 =>
         Put_Line("Procedure Foo caught exception U0");
   end Foo;
begin
   for I in 1..2 loop
      Foo;
   end loop;
end Exceptions_From_Nested_Calls;