ArrayStoreException in Java

ArrayStoreException in Java occurs whenever an attempt is made to store the wrong type of object into an array of objects. The ArrayStoreException is a class which extends RuntimeException, which means that it is an exception thrown at the runtime.

Class Hierarchy:

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

Constructors of ArrayStoreException:



  1. ArrayStoreException(): Constructs an ArrayStoreException instance with no detail message.
  2. ArrayStoreException(String s): Constructs an ArrayStoreException instance with the specified message s.
  • When does ArrayStoreException occurs?

    ArrayStoreException in Java occurs whenever an attempt is made to store the wrong type of object into an array of objects.

    Below example illustrates when does ArrayStoreException occur:

    Since Number class is a superclass of Double class, and one can store an object of subclass in super class object in Java. Now If an integer value is tried to be stored in Double type array, it throws a runtime error during execution. The same thing wouldn’t happen if the array declaration would be like:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class GFG {
      
        public static void main(String args[])
        {
      
            // Since Double class extends Number class
            // only Double type numbers
            // can be stored in this array
            Number[] a = new Double[2];
      
            // Trying to store an integer value
            // in this Double type array
            a[0] = new Integer(4);
        }
    }

    chevron_right

    
    

    Runtime Exception:



    Exception in thread “main” java.lang.ArrayStoreException: java.lang.Integer
    at GFG.main(GFG.java:13)

  • How to handle with ArrayStoreException?

    One can use try-catch block in Java to handle ArrayStoreException.

    Below example illustrates how to handle ArrayStoreException:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class GFG {
      
        public static void main(String args[])
        {
      
            // use try-catch block
            // to handle ArrayStoreException
            try {
      
                Object a[] = new Double[2];
      
                // This will throw ArrayStoreException
                a[0] = 4;
            }
      
            catch (ArrayStoreException e) {
      
                // When caught, print the ArrayStoreException
                System.out.println("ArrayStoreException found: "
                                   + e);
            }
        }
    }

    chevron_right

    
    

    Output:

    ArrayStoreException found: java.lang.ArrayStoreException: java.lang.Integer



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.