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.




record U0 type Exception
end
 
record U1 type Exception
end
 
program Exceptions
 
    function main()
        foo();
    end
 
    function foo()
        try
            bar();
        onException(ex U0)
            SysLib.writeStdout("Caught a U0 with message: '" :: ex.message :: "'");
        end
        bar();
    end
 
    function bar()
        baz();
    end
 
    firstBazCall boolean = true;
    function baz()
        if(firstBazCall)
            firstBazCall = false;
            throw new U0{message = "This is the U0 exception"};
        else
            throw new U1{message = "This is the U1 exception"};
        end
    end
end