Here is the exact code I was using to test (along with Danilo's
"Try/Catch" macros). It's sloppy, but quick and dirty for testing...
Make sure to comment out either the call to the RunTry() procedure, or the call to the RunOnErr() procedure when testing. Do not run both procedures at the same time as the two methods should never be mixed. Additionally, when testing each procedure, you may comment out the call to the PokeS() procedure to see the different affects of what occurs.
- The Try/Catch error handler returns a value following an error, but does not raise the correct error number. This cannot work for me.
- The OnError() error handler raises the correct error number, but it does not allow the function to return a value to its caller. This (especially) cannot work for me, either. Also note that the label is executed twice when using OnErrorGoto(?ErrHndlr). However, the procedure correctly only executes once when using OnErrorCall(@DoErrHandlr()).
I would prefer to use the OnError() style of error handling over Try/Catch if I can figure out how to force my functions to return a value to their caller. So, if anyone can point out what I'm doing wrong here, your assistance will be greatly valued. However, it would also be nice if we can get the Try/Catch style ironed out as well. Someone else may prefer it over OnError().
Test Code
CompilerIf #PB_Compiler_Debugger
CompilerError "please disable debugger"
DisableDebugger
CompilerEndIf
Procedure DoErrHandlr()
MessageRequester("ShowMessage_OnError()", "Oops! An error occured here." + #CRLF$ + #CRLF$ + "Error Number: " + Str(ErrorCode())) ;Display the error code to user...
EndProcedure
Procedure.i ShowMessage_Try(Text.s)
Protected.i intRslt ;Retains the value to be returned by the function...
Try
MessageRequester("ShowMessage_Try()", Text)
PokeS(123, "The quick brown fox jumped over the lazy dog.") ;Purposely cause a stack error...
intRslt = 1000 ;Indicate the function successfully executed...
Catch
MessageRequester("ShowMessage_Try()", "Oops! An error occured here which should have returned the number [-1073741819] but returned the incorrect number of:" + #CRLF$ + #CRLF$ + "Error Number: " + Str(ErrorCode())) ;Display the error code to user...
intRslt = 2000 ;Indicate the function was not successful...
EndTry
MessageRequester("ShowMessage_Try()", "If you see this message, the command excuted correctly regardless of whether an error occurred.")
ProcedureReturn intRslt ;Return whether the function successfully executed...
EndProcedure
Procedure.i ShowMessage_OnErr(Text.s)
Protected.i intRslt ;Retains the value to be returned by the function...
OnErrorGoto(?ErrHndlr)
; OnErrorCall(@DoErrHandlr())
MessageRequester("ShowMessage_OnError()", Text)
PokeS(123, "The quick brown fox jumped over the lazy dog.") ;Purposely cause a stack error...
intRslt = 1000 ;Indicate the function successfully executed...
ProcedureReturn intRslt ;Return whether the function successfully executed...
ErrHndlr: ;THIS LABEL IS EXECUTED TWICE WHEN AN ERROR OCCURS...
MessageRequester("ShowMessage_OnError()", "Oops! An error occured here." + #CRLF$ + #CRLF$ + "Error Number: " + Str(ErrorCode())) ;Display the error code to user...
ProcedureReturn 2000 ;Return that the function was not successful...
EndProcedure
Procedure RunTry()
Protected.i intRslt ;Retains the execution result of the 'ShowMessage()' function...
intRslt = ShowMessage_Try("Hello, World!") ;Call the function to show the message...
If intRslt = 1000
MessageRequester("RunTry()", "The called function successfully returned a value of True to indicate no error occurred.")
ElseIf intRslt = 2000
MessageRequester("RunTry()", "The called function successfully returned a value of False to indicate an error did occur.")
Else
MessageRequester("RunTry()", "The called function did not return a value following an error. This is unacceptable.")
EndIf
EndProcedure
Procedure RunOnErr()
Protected.i intRslt ;Retains the execution result of the 'ShowMessage()' function...
intRslt = ShowMessage_OnErr("Hello, World!") ;Call the function to show the message...
If intRslt = 1000
MessageRequester("RunOnErr()", "The called function successfully returned a value of True to indicate no error occurred.")
ElseIf intRslt = 2000
MessageRequester("RunOnErr()", "The called function successfully returned a value of False to indicate an error did occur.")
Else
MessageRequester("RunOnErr()", "The called function did not return a value following an error. This is unacceptable.")
EndIf
EndProcedure
If OpenConsole() ;If a console window is successfully created - begin the program...
Define.i intRslt ;Retains the execution result of the 'ShowMessage()' function...
EnableGraphicalConsole(#True) ;Turn on graphical text mode...
ConsoleTitle("PureBasic: Console Test") ;Set the console windows's caption...
; RunTry() ;Test the Try/Catch error handling process...
RunOnErr() ;Test the OnError() error handling process...
CloseConsole() ;Close the console window...
EndIf
End ;Completely end program...