exceptions

java.lang.ArrayStoreException Example

In this example we will discuss about ArrayStoreException. This exception is thrown when there has been made an attempt to store the wrong type of object into an array of objects.

The ArrayStoreException extends RuntimeException, which means that it is an exception thrown at the runtime, so the try-catch block for this exception is not required.

ArrayStoreException exists since JDK1.0.
 
 

The structure of ArrayStoreException

Constructor:

  • ArrayStoreException()

    Constructs an ArrayStoreException instance with no detail message.

  • ArrayStoreException(String s)

    Constructs an ArrayStoreException instance with the specified detail message.

The ArrayStoreException in Java

To see when this exception is thrown, create a simple class called ExceptionThrownExample and put this source code on it:

    ExceptionThrownExample.java

    package com.javacodegeeks.examples;
    
    public class ExceptionThrownExample {
    	public static void main(String... args) {
    		Object[] s = new Integer[4];
    		s[0] = 4.4;
    	}
    
    }
    

    In this example, I created an Integer array and tried to put 4.4 as its first element. While this is a non-sense (4.4 is not an integer), the compiler doesn’t think it is wrong, and doesn’t generate any error or warning during the compilation.

    But, when I run it, I get this exception:

    Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
    	at com.javacodegeeks.examples.ExceptionThrownExample.main(ExceptionThrownExample.java:6)
    

    The same thing wouldn’t happen if the fifth line would be like:

    Integer[] s = new Integer[4];
    

    In this case, an error would occur and the compilation would fail. This happens because in the first case, the array is an array of Object, and 4.4 is an object. In the second case, we explicitly say that the array is an array of Integer objects, and 4.4 is not an instance of Integer, hence the error.

    The same exception would be thrown even if we try store an array of objects (any kind of objects). So create a new class called ObjectsArray:

    ObjectsArray.java

    package com.javacodegeeks.examples;
    
    public class ObjectsArray {
    
    	public static void main(String[] args) {
    		Object[] s = new String[4];
    		s[0] = new Object[5];
    	}
    
    }
    

    Java arrays are in fact objects, so even this would not throw an error during compile time. But, when it is run, the same exception is thrown:

    Exception in thread "main" java.lang.ArrayStoreException: [Ljava.lang.Object;
    	at com.javacodegeeks.examples.ObjectsArray.main(ObjectsArray.java:7)
    

    How to deal with ArrayStoreException

    Whenever you see this exception, it means that you have been storing a wrong kind of data type in an array. One thing that may solve this, is the usage of the proper type, or even casting to the proper type.

    A way to prevent this exception, is to use a less generic data type in your arrays. If the above example fits, it would be a good idea not to use Object as the array type, but maybe Integer or String, depending to the use case.

    Download Code

    Download
    You can download the full source code of this example here : ArrayStoreExceptionExample

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button