I'm trying to write a matrix calculator for decomposition. However, there are some cases in the matrix calculator where I don't want the system to return anything, but just print out an error message.
I have tried to do this by replacing the return call with a throw new Exception method, but it clearly doesn't seem to be working because: 1. there needs to be some sort of catch/throws implemented and 2. there is still a need for a return statement.
public double[][] multiply(Matrix other) {
if(getCols() == other.getRows()) {
double[][] mult = new double[getRows()][other.getCols()];
for(int r = 0; r < mult.length; r++) {
for(int c = 0; c < mult[0].length; c++) {
mult[r][c] = mult(m1[r],findCol(other,c));
}
}
return mult;
}
else {
throw new MatrixException("Multiply");
}
}
So as can be seen by the else
statement, in place of a return
statement, it is replaced with throw new MatrixException("Multiply")
. This simply returns a String statement, but the code will not compile. Is there any way to use a try
-catch
method to throw the Exception without needing to implement a return? Also, yes this is the first time I'm asking a question, so I'm still not fully familiar with the question formatting techniques.