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.