Sunday, 30 June 2019

Exceptions | Difference between ArrayIndexOutfOBounds and ArrayStoreException

ArrayIndexOutOfBoundsException
Since JDK1.0

ArrayIndexOutOfBoundsException occurs when your code tries to access an invalid index for a given array e.g. negative index or higher index than length - 1.

java.lang.Object
   java.lang.Throwable
      java.lang.Exception
         java.lang.RuntimeException
            java.lang.IndexOutOfBoundsException
               java.lang.ArrayIndexOutOfBoundsException


  • ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException.
  • 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.

ArrayStoreException
Since JDK1.0
ArrayStoreException occurs when you have stored an element of type other than the type of array.

java.lang.Object
   java.lang.Throwable
      java.lang.Exception
         java.lang.RuntimeException
            java.lang.ArrayStoreException


  • ArrayStoreException extends RuntimeException
  • Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.

Example
Object x[] = new String[3];
x[0] = new Integer(0);

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...