The MAXON API ALIASES uses a custom error reporting system. In case of an error, a function can return an error object. The return value and the error object are stored in a maxon::Result object. Within a function scope it is also necessary to handle errors returned by sub-functions.
A function that returns a maxon::Result object can either return just the plain return value or a complex error object. This error object can contain detailed information on the error that occurred. For more details on the maxon::Result class see Error Result. For a list of default error types see Error Types.
Functions that do not return any value can return Result<void>:
It is possible to create an error object before it is needed to increase the performance in speed critical tasks:
Within a given error scope one must define how errors returned by sub-functions are handled.
Best practice to handle errors is typically to declare an error scope and to simply return the error returned by a sub-function:
In such an error scope one can simply return from the function if an error occurred in a sub-function:
If a simple error scope is declared with iferr_scope, the attribute iferr_return will return from the function returning the detected error. In contrast, iferr_scope_handler allows to define a function that is called before iferr_return will return.
Compare also Finally.
It is also possible to simply check if a called function returned an error or not.
err
that can be returned.If a new error is created within a function it is typically just returned. If one wants iferr_scope_handler to be called before one must use iferr_throw:
In some cases an error can be ignored. Such cases must be marked and explained explicitly:
Error handling can be limited to a sub-scope within a function by using a lambda:
The attribute iferr_return allows to return from a function when an error was detected. This could lead to issues with resources that have to be managed and freed within the function scope. For example a certain resource must be freed when the function is ending. Allocated objects and memory are best handled using References.
For more complex situations one can use either iferr_scope_handler or finally: