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.




' FB 1.05.0 Win64
 
Enum ErrorTypes
  U0 = 1000
  U1
End Enum
 
Function errorName(ex As ErrorTypes) As String
  Select Case As Const ex
    Case U0
      Return "U0"
    Case U1
      Return "U1"
  End Select
End Function
 
Sub catchError(ex As ErrorTypes)
   Dim e As Integer = Err '' cache the error number
   If e = ex Then
     Print "Error "; errorName(ex); ", number"; ex; " caught"
   End If
End Sub
 
Sub baz()
  Static As Integer timesCalled = 0 '' persisted between procedure calls
  timesCalled += 1
  If timesCalled = 1 Then
    err = U0
  Else
    err = U1
  End if 
End Sub
 
Sub bar()
  baz
End Sub
 
Sub foo()
  bar 
  catchError(U0) '' not interested in U1, assumed non-fatal
  bar 
  catchError(U0)
End Sub
 
Foo
Print
Print "Press any key to quit"
Sleep