Manually adding IndexOutOfBounds Exception
I'm trying to manually throw an index out of bounds exception for an array. I know that to throw a regular exception, I can do something like:
if(x>array.length){
throw new Exception("Bad choice!");
}
But how can I do the index out of bounds ex开发者_如何学JAVAception?
Thanks
throw new IndexOutOfBoundsException("Index " + x + " is out of bounds!");
The API is a very good resource. Checking it before posting a question could save you the time of doing so.
You can do:
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Index out of bound");
Why is this not working?
精彩评论