Invalid Input
An
invalid input error is usually caused by the final end user. For example, you might use class
RWGenFact<T> to construct an LU factorization from a matrix that turns out to be singular. It is a mean-spirited program that simply aborts when given such a matrix. It is preferable if the factorization’s validity can be explicitly checked, to be rejected or corrected.
The line between a violated precondition and an invalid input can be unclear. For example, the rules could say, "Don’t give me a singular matrix," and then the programmer is responsible for detecting the matrix’s singularity before handing it to the
RWGenFact<T> constructor. Of course, this is a lot of work, and in this example the
RWGenFact<T> class is better equipped than the caller to determine the singularity of the matrix. Hence, this is an approach that is generally not taken.
How are these kinds of errors dealt with? Generally, either by returning a special value, such as a nil pointer, or by using a member function that tests the validity of the resultant object. The following code continues the
RWGenFact<T> example:
// This matrix is hopelessly singular:
RWGenMat<double> a(8, 8, 1);
RWGenFact<double> fact(a);
if( fact.fail() ) cerr << "Non-factorable matrix"
else {
// ... Use it
}
The above program prints out
Non factorable matrix. Like many other classes, class
RWGenFact<T> has several member functions for testing its validity.