Macro functions or Try/Catch for error handling
Posted: Thu May 28, 2009 6:01 pm
PureBasic does not have an equivalent construct for the Try/Catch method of error handling. It does not support object-oriented paradigms and more closely resembles the procedural "C programming language".
C handles errors through return values or errorno. Macro functions are the ideal solution for handling these types of error values because they can also return the original value of a function in case of special circumstances or if no real error has occurred.
Here is a simple procedural error handler in PureBasic, for example:
PureBasic's macros are perfectly fine if the function being called has no return value:
But if it does then this or some similar form of it is required:
But it makes large and complex code more difficult to read with the repetition of these essential functions.
Certainly, here is an option to keep things on "one line":
But this can be error prone, impossible to read, and difficult to maintain as now every return value can have multiple representations and also duplicate variable names. Considering lines cannot be split in PureBasic, it's possible for the "ErrorHandler" portion to be cut off past the edge of the screen and require horizontal scrolling to adjust.
There will also now be to unique cases to be handled where the function returns a value and if it does not. The calling convention will be completely different and also error-prone and confusing:
Additionally, if the second method is used and the function goes off the screen then there is no way to identify that the error is even being handled without scrolling horizontally.
Macros for error handling are essential here but can be impossible to read and are difficult to maintain. Because PureBasic follows the procedural paradigm I hope that the PureBasic team will strongly consider supporting macro functions. It's the only way to elegantly handle these errors without adding additional code to handle each return value and unless there is a future for Try/Catch exception handling with adequate, architecture-independent supporting functions (register/call stack dump with debugger support), it is the obvious solution.
C handles errors through return values or errorno. Macro functions are the ideal solution for handling these types of error values because they can also return the original value of a function in case of special circumstances or if no real error has occurred.
Here is a simple procedural error handler in PureBasic, for example:
Code: Select all
Macro ErrorHandler(Result)
CompilerIf Defined(LastError,#PB_Variable)
CompilerElse
Protected LastError
CompilerEndIf
If Result < 0
ReportError(LastError)
ProcedureReturn LastError ;/ Exit procedure/thread
EndIf
EndMacro
Code: Select all
ErrorHandler(SomeFunction())
Code: Select all
Result = SomeFunction()
ErrorHandler(Result)
Certainly, here is an option to keep things on "one line":
Code: Select all
Result = SomeFunction(): ErrorHandler(Result)
There will also now be to unique cases to be handled where the function returns a value and if it does not. The calling convention will be completely different and also error-prone and confusing:
Code: Select all
ErrorHandler(SomeFunction1())
Result = SomeFunction2(): ErrorHandler(Result)
Macros for error handling are essential here but can be impossible to read and are difficult to maintain. Because PureBasic follows the procedural paradigm I hope that the PureBasic team will strongly consider supporting macro functions. It's the only way to elegantly handle these errors without adding additional code to handle each return value and unless there is a future for Try/Catch exception handling with adequate, architecture-independent supporting functions (register/call stack dump with debugger support), it is the obvious solution.