Try, Catch, Finally: Catch is run without exception

edited November 2017 in Questions about Code
void instantiateMatrix(){
  boolean errorFlag = false;

  try {
    Matrix m = new Matrix(new String("4;4;2;5"), 2, 2);
    m.showMatrix();
  } catch(java.lang.RuntimeException e) {
    errorFlag = true;
  } finally {
    if (!errorFlag){
      println("Test 1: Success");
    } else{
      println("Test 1: Failure");
    }
  }

I'm trying to unit test my matrix math class (and other classes), and this code snippet is testing the overloaded constructor of the Matrix. The problem is that even if no exception occurs ( a runtime error doesn't occur) the catch statement is still executed.

Tagged:

Answers

  • What do you mean by collisions still occur?

    This would be a lot easier to see what was going on with the inclusion of 3 println statements.

  • @koogs sorry for the typo, I fixed it now

  • Answer ✓

    so, after line 7 add e.printStackTrace();

    tell us what it says.

    runnable examples are always better. here the problem looks to be in you matrix class which we don't have.

Sign In or Register to comment.