Search code examples
javaarraysthrowindexoutofboundsexception

How to throw ArrayIndexOutOfBoundsException?


I have a method that checks spots in a 2D array, and it also checks if they are null. I want to throw the ArrayIndexOutOfBoundsException because I already check for null.

I tried adding throws ArrayIndexOutOfBoundsException after declaring the method, but it doesn't work. How do I do this?


Solution

  • throws in the method definition says that the method can throw that exception. To actually throw it in the method body, use throw new ArrayIndexOutOfBoundsException();