Utilities
Error Checking
One important aspect of code that has been ignored thus far is the checking of return values from function calls. Error checking is crucial to catching both mistakes in code, mistakes in input files, and even library bugs. EGAD Library has a standard form of error reporting that should be taken advantage of in any client application.
There is an enumeration called ERROR_TYPE that many library functions use to report their exit status. You have already been introduced to this type during the discussion of minimizers. The Minimize() function returns an ERROR_TYPE.
The most common value for ERROR_TYPE, and the one you should hope to expect from most functions, is ETYPE_OK. This means that the function exited normally. You can check for this directly, or you can call the global function Failed() to check if a function has failed.
Example 5.1: Checking the error status of a function call
// Assume we have a function with the following prototype
ERROR_TYPE MyFunction();
// Execute the function and record its return value
ERROR_TYPE retval = MyFunction();
// Check if the function returned successfully
if (Failed(retval)){
std::cout << "Error!" << std::endl;
}
// We could also check the return value directly. This gives us
// finer control over what return values to catch.
if (retval != ETYPE_OK){
std::cout << "Error!" << std::endl;
}
The meaning of other return values, such as ETYPE_OUT_OF_RANGE or ETYPE_UNKNOWN are not defined, globally. Their names are meant to be hints about the origin of the error, but the function documentation should be consulted to understand what the actual meaning of an error is.
Important
A few functions, like Minimize(), use different ERROR_TYPE values to report different behaviors that aren't necessarily errors. Watch out for this, as well. When this happens, it is clearly marked in the function documentation.