开发者

Manually adding IndexOutOfBounds Exception

开发者 https://www.devze.com 2023-04-02 08:40 出处:网络
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:

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消