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.




type Exception1 = class (Exception) end;
type Exception2 = class (Exception) end;
 
procedure Baz(i : Integer);
begin
   if i=0 then
      raise new Exception1('Error message 1')
   else raise new Exception2('Error message 2');
end;
 
procedure Bar(i : Integer);
begin
   Baz(i);
end;
 
procedure Foo;
var
   i : Integer;
begin
   for i:=0 to 2 do begin
      try
         Bar(i);
      except
         on E : Exception1 do
            PrintLn(E.ClassName+' caught');
      end;
   end;
end;
 
Foo;