How to handle a fatal error elegantly and legibly
Posted: Wed Jan 30, 2008 10:18 am
I've always been annoyed with having to check for errors in my code by "If Not .. Error .. Else .. Endif" every place necessary because sometimes it would muck up the flow of my code or cause some other problem and it wasn't obvious to be an error routine at a glance.
Here is a really neat solution I've come up with for handling errors elegantly and how to fail to cleanup or continue while being obvious, unobtrusive, and simple to use!
One thing I wish I could do would be to use function overloading for all basic types and type-check them inside the procedure. But I know that just won't happen.
Here is a really neat solution I've come up with for handling errors elegantly and how to fail to cleanup or continue while being obvious, unobtrusive, and simple to use!
One thing I wish I could do would be to use function overloading for all basic types and type-check them inside the procedure. But I know that just won't happen.

Code: Select all
#Fatal=1
Procedure SendError(ErrMsg.s,Fatal)
MessageRequester("Application Name - Error",ErrMsg.s)
If Fatal
; Perform cleanup
End
EndIf
EndProcedure
Procedure FAILED(Var,ErrMsg.s,Fatal)
If Not Var
SendError(ErrMsg.s,Fatal)
ProcedureReturn 1
EndIf
EndProcedure
FileID=ReadFile(#PB_Any,"C:\NonExistantFile.ext")
If FAILED(FileID,"Couldn't open the file!",#Fatal)
; Do something here if the error is not fatal
EndIf
Debug 2 ; Not run