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.




 
const class U0 : Err
{
  new make () : super ("U0") {}
}
 
const class U1 : Err
{
  new make () : super ("U1") {}
}
 
class Main
{
  Int bazCalls := 0
 
  Void baz ()
  {
    bazCalls += 1
    if (bazCalls == 1) 
      throw U0()
    else 
      throw U1()
  }
 
  Void bar ()
  {
    baz ()
  }
 
  Void foo ()
  {
    2.times
    {
      try
      {
        bar ()
      }
      catch (U0 e)
      {
        echo ("Caught U0")
      }
    }
  }
 
  public static Void main ()
  {
    Main().foo
  }
}
 