Error Types
For different situations different error classes exist. These different classes help to identify the nature of the error and allow to store additional information.
The default error types are defined in the core.framework.
Errors about incorrect function arguments:
Errors about incorrect function implementations:
Errors about incorrect program flow:
Specialised Errors:
General Errors:
//---------------------------------------------------------------------------------------- // This function replaces each element of the given array with its square root. // @param[in,out] numbers An array of maxon::Float32 values. // @return maxon::OK on success. //---------------------------------------------------------------------------------------- static maxon::Result<void> SquareRootElements( maxon::BaseArray<maxon::Float32> & numbers) { iferr_scope ;
// prepare AggregatedError maxon::AggregatedError agg( MAXON_SOURCE_LOCATION );
// loop through all elements of the given array const maxon::Int count = numbers. GetCount (); for ( maxon::Int i = 0; i < count; ++i) { maxon::Float32 & number = numbers[i];
// if any errors were found, return the collected errors if (agg.GetCount()) return agg;
// This example reads the AggregatedError returned by a function // and prints all sub-errors. const maxon::Result<void> res = SquareRootElements(data);
// check for errors if (res == maxon::FAILED ) { const maxon::Error error = res. GetError ();
// check for AggregatedError if (error.IsInstanceOf< maxon::AggregatedError >()) { // get AggregatedError const maxon::AggregatedError agg = maxon::Cast<maxon::AggregatedError>(error);
// loop through all sub-errors for ( const maxon::Error subError : agg) { DiagnosticOutput ( "Sub-Error: @" , subError); } } }
All error types are based on maxon::ErrorInterface . It is possible to create custom error types based on that interface. See also 接口基础 .
A custom error interfaces is based on the maxon::ErrorInterface (见 Error Class ):
// This example shows the declaration of a custom error type. // The custom error is able to store a custom error code.// --------------------------------------------------------------------- // Custom error class that stores an error code. // --------------------------------------------------------------------- class ExampleErrorInterface : MAXON_INTERFACE_BASES (maxon::ErrorInterface) { MAXON_INTERFACE (ExampleErrorInterface, MAXON_REFERENCE_COPY_ON_WRITE , "net.maxonexample.error.example" ); public : MAXON_ADD_TO_COPY_ON_WRITE_REFERENCE_CLASS ( void Create( MAXON_SOURCE_LOCATION_DECLARATION , maxon::Int errorCode) { * static_cast< typename S::DirectlyReferencedType::Hxx1::ReferenceClass* > ( this ) = S::DirectlyReferencedType::Hxx1::ErrObj::GetInstance()(); typename S::DirectlyReferencedType::Ptr e = this->MakeWritable( false ).GetPointer(); e.SetLocation( MAXON_SOURCE_LOCATION_FORWARD ); e.SetCustomErrorCode(errorCode); } );
// custom methods
// --------------------------------------------------------------------- // Stores an custom error code. // --------------------------------------------------------------------- MAXON_METHOD void SetCustomErrorCode( maxon::Int errorCode);
// --------------------------------------------------------------------- // Returns the stored custom error code. // --------------------------------------------------------------------- MAXON_METHOD maxon::Int GetCustomErrorCode() const ; };
The registration or implementation of the custom error type can be simplified with these macros:
// custom methods MAXON_METHOD void SetCustomErrorCode( maxon::Int errorCode) { _errorCode = errorCode; } MAXON_METHOD maxon::Int GetCustomErrorCode() const { return _errorCode; } private : maxon::Int _errorCode; };
// register implementation MAXON_COMPONENT_OBJECT_REGISTER (ExampleErrorImpl, ExampleErrorObject);
The new error type can now easily be used:
// This example creates and returns an instance of the custom error. static maxon::Result<void> CustomOperation() { // execute operation const maxon::Int res = Operate();// if no success, return error if (res == -1) return ExampleError( MAXON_SOURCE_LOCATION , res); return maxon::OK ; }