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.




$ include "seed7_05.s7i";
 
const EXCEPTION: U0 is enumlit;
const EXCEPTION: U1 is enumlit;
 
const proc: baz (in integer: num) is func
  begin
    if num = 1 then
      raise U0;
    else
      raise U1;
    end if;
  end func;
 
const proc: bar (in integer: num) is func
  begin
    baz(num);
  end func;
 
const proc: foo is func
  local
    var integer: num is 0;
  begin
    for num range 1 to 2 do
      block
        bar(num);
      exception
        catch U0: writeln("U0 catched");
      end block;
    end for;
  end func;
 
const proc: main is func
  begin
    foo;
  end func;