try catch ArrayIndexOutOfBoundsException?

I don’t see an array anywhere in your code, so that’s maybe why the try block isn’t catching anything (I assume there is an array in one of the called methods?). Also, you really, really shouldn’t allow your program to read outside the bounds of an array. That’s just bad design. That being said, here is how you would catch the exception in the clearest way I can think of:

try {
    array[index] = someValue;
}
catch(ArrayIndexOutOfBoundsException exception) {
    handleTheExceptionSomehow(exception);
}

Or do as @Peerhenry suggests and just throw a new Exception if the indices aren’t correct, which would be a much better design.

Leave a Comment