-2

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.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • 'throw new MatrixException("Multiply");' -> this simply returns a String statement. No, it doesn't return a String statement. It throws an Exception. What compile time problem do you get? – Stultuske May 28 '19 at 11:25
  • Please [edit] your question and include the class `MatrixException`. Also clearly mention if you get any error, and if you do **what** the error message exactly says. – Lino May 28 '19 at 11:27
  • Also this `mult[r][c] = mult(m1[r],findCol(other,c));` should not compile. You can't invoke an array. Please provide a [reprex] – Lino May 28 '19 at 11:29
  • Just one note about usability. Instead of throwing `new MatrixException("Multiply")` it will be better throw something like `throw new MatrixException(format("Multiply error: getCols() [%d] not equals other.getRows() [%d]", getCols(), other.getRows()))` – Bor Laze May 28 '19 at 11:53

1 Answers1

3

You could inform the caller of multiply that an exception can be thrown by changing your method like this:

public double[][] multiply(Matrix other)throws MatrixException{}

So the method now would be:

public double[][] multiply(Matrix other) throws MatrixException {  // tells the method throws an exception
    if(getCols() == other.getRows()) {
        // ...
        return <your_valid_return_here>
    }
    throw new MatrixException("Multiply");  // inform the caller there is an exception if the previous condition is not met
}

Also, bear in mind what type of exception is MatrixException (checked or unchecked) in order to follow this approach. If checked, the caller will be forced to handle it in the calling code (or report its code can throw the exception), not being like this if it is unchecked.

Additional reading:

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80