Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ArrayIndexOutOfBoundsException and IndexOutOfBoundsException?

What are the use cases in which we should use ArrayIndexOutOfBoundsException and `IndexOutOfBoundsException one over another?

like image 767
quintin Avatar asked Dec 14 '15 11:12

quintin


2 Answers

IndexOutOfBoundsException :Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException are two classes, which have implemented IndexOutOfBoundsException.

ArrayIndexOutOfBoundsException: Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

StringIndexOutOfBoundsException:Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.

like image 107
Ravindra babu Avatar answered Sep 22 '22 10:09

Ravindra babu


IndexOutOfBoundsException is the super class of ArrayIndexOutOfBoundsException (thrown when accessing invalid index in a array) and StringIndexOutOfBoundsException (thrown when accessing invalid index in a String).

Instances of the base class IndexOutOfBoundsException itself are thrown when accessing invalid indices of Lists.

The Javadoc of some methods that throw IndexOutOfBoundsException or its sub-classes contains the base class. For example, String.charAt is documented to be throwing IndexOutOfBoundsException when it actually throws the sub-class StringIndexOutOfBoundsException.

like image 38
Eran Avatar answered Sep 22 '22 10:09

Eran