Page 1 of 1

Throw with Rings TryError Library (Exception Handling)

Posted: Sun Mar 13, 2005 3:10 pm
by Leo
With the TryError (Try and Catch) library of Rings it is also easy to implement Throw. With Throw you can trigger an exception in a (nested) procedure and process the exception at the mainlevel (the GUI level).

Below an example to play around. The Try and Catch library can be found at http://www.purearea.net. See also the announcement forum viewtopic.php?t=7258&highlight=tryerror

Code: Select all

Global ErrorMsg.s
ErrorMsg=""

Procedure AppendErrorMsg(Tekst.s)
  If ErrorMsg=""
    ErrorMsg="Error"
  EndIf
  ErrorMsg+Tekst
EndProcedure

Procedure DisplayErrorMsg()
  If ErrorMsg=""
    ErrorMsg="Unknown Error"
  EndIf  
  MessageRequester("Error !",ErrorMsg)
  ErrorMsg=""
EndProcedure

Procedure Throw(Tekst.s)
  If Len(Tekst)>0
    ErrorMsg=Tekst
  EndIf  
  !INT 3
EndProcedure

Procedure Proc8()
  handle=ReadFile(1,"TstFile.txt")
  If handle Or Throw("TstFile not found")
    CloseFile(1)
  EndIf
  a=3
  b=0
  If b Or Throw("Divide by zero")
    a/b
  EndIf
  Throw("Other Error")
EndProcedure
  
Procedure Proc7()
  E=0
  If TryError()
    a=4
    b=0
    a/b
  Else
    E=1
  EndIf:EndTryError()
  If E=1
    Throw("")
  EndIf  
EndProcedure
  
Procedure Proc6()
  E=0
  If TryError()
    Proc7()
  Else  
    AppendErrorMsg(" in Proc6")
    E=1
  EndIf:EndTryError()
  If E=1
    Throw("")
  EndIf  
EndProcedure  

Procedure Proc5()
  Proc6()
EndProcedure  

Procedure Proc4()
  Proc5()
EndProcedure  

Procedure Proc3()
  E=0
  If TryError()
    Throw("File not found")
    a=3
    b=4
  Else  
    AppendErrorMsg(" in Proc3")
    E=1
  EndIf:EndTryError()
  If E=1
    Throw("")
  EndIf  
EndProcedure

Procedure Proc2()
  E=0
  If TryError()
    Proc3()
    a=3
    b=0
    a=a/b
  Else  
    AppendErrorMsg(" in Proc2")
    E=1
  EndIf:EndTryError()
  If E=1
    Throw("")
  EndIf  
EndProcedure

Procedure Proc1()
  E=0
  If TryError()
    Proc2()
    a=3
    b=0
    If b=0
      Throw("Divide by zero")
    EndIf  
    a=a/b
  Else  
    AppendErrorMsg(" in Proc1")
    E=1
  EndIf:EndTryError()
  If E=1
    Throw("")
  EndIf  
EndProcedure

;OnErrorGosub(@ErrorHandling())
If OpenWindow(0,10,10, 300, 200, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget ,"Test Exception Handling") And CreateGadgetList(WindowID(0))
  ButtonGadget(1,10,10,100,20,"Button1")
  ButtonGadget(2,10,40,100,20,"Button2")
  ButtonGadget(3,10,70,100,20,"Button3")
  ButtonGadget(4,10,100,100,20,"Button4")
  Repeat
    EventID.l = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadgetID()
        Case 1
          If TryError()
            c=9
            a=1
            If a Or Throw("Divide by zero") : EndIf  
            c/a
            MessageRequester("","Before processing Button1")
            Proc1()
            MessageRequester("","After processing Button1")
          Else
            AppendErrorMsg(" by clicking Button1")
            DisplayErrorMsg()
         EndIf:EndTryError()  
        Case 2 
          If TryError()
            MessageRequester("","Before processing Button2")
            Proc2()
            MessageRequester("","After processing Button2")
          Else
            AppendErrorMsg(" by clicking Button2")
            DisplayErrorMsg()
          EndIf:EndTryError()  
        Case 3 
          If TryError()
            MessageRequester("","Before processing Button3")
            Proc4()
            MessageRequester("","After processing Button3")
          Else
            AppendErrorMsg(" by clicking Button3")
            DisplayErrorMsg()
          EndIf:EndTryError()  
        Case 4 
          If TryError()
            MessageRequester("","Before processing Button4")
            Proc8()
            MessageRequester("","After processing Button4")
          Else
            AppendErrorMsg(" by clicking Button4")
            DisplayErrorMsg()
          EndIf:EndTryError()  
      EndSelect
    EndIf
    
  Until EventID = #PB_EventCloseWindow
EndIf
End

Posted: Mon Mar 14, 2005 9:26 am
by Rings
nice one :wink: