javaloopsthrowable

Loop continuing even after hitting return statement


I have the following method where I wan to keep checking if a nested exception matches an IndexOutOfBoundsException unless the nested exception is the same as the previous one.

It seems to do the right thing where in my test, the first exception is of type NullPointerException thus moving on to the next. The next exception is as expected, an IndexOutOfBoundsException.

When this happens I want to return true which I expect to break me out of the loop. It seems to be happening as expected where I do land on 'return true'. But after that, the loop keeps going.

What am I missing. How is it keeping on going even after returning true?

public boolean test(Throwable throwable) {

    do {
        if(throwable instanceof IndexOutOfBoundsException) {
            return true; // I do land here thus expecting to get out of loop and this method.
        }
        this.test(throwable.getCause());
    } while (throwable.getCause() != throwable);

    return false;
}

The test against it simulating nested exception.

@Test
void testWithNestedException() {
    NullPointerException nullPointerException = new NullPointerException();
    IndexOutOfBoundsException indexOutOfBoundsException = new IndexOutOfBoundsException();
    Throwable nestedException = nullPointerException.initCause(indexOutOfBoundsException);
    assertTrue(someClass.test(nestedException));
}

Solution

  • You are mixing recursion with looping. Here, a simple loop that updates the exception being tested should do the trick:

    public boolean test(Throwable throwable) {
        Throwable t = throwable;
        do {
            if (throwable instanceof IndexOutOfBoundsException) {
                return true;
            }
            t = t.getCause();
        } while (t.getCause() != t);
    
        return false;
    }