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.




constant U0 = 0,
         U1 = 1
 
integer count = 0
 
procedure baz()
    count += 1
    if count=1 then
        throw(U0,{{"any",{{"thing"},"you"}},"like"})
    else
        throw(U1)
    end if
end procedure
 
procedure bar()
    baz()
end procedure
 
procedure foo()
    for i=1 to 2 do
        try
            bar()
        catch e
            if e[E_CODE]=U0 then
                ?e[E_USER]
            else
                throw(e)    -- (terminates)
            end if
        end try
        puts(1,"still running...\n")
    end for
    puts(1,"not still running...\n")
end procedure
 
foo()
