java.lang.ArrayStoreException in Java
On this page
Java.lang.ArrayStoreException Class
Creating Objects
Default Constructor: This creates an instance without a message.
ArrayStoreException ex1 = new ArrayStoreException();
Constructor with Message:passing a custom error message.
ArrayStoreException ex2 = new ArrayStoreException("Incompatible type stored in array");
ArrayStoreException Class Example
public class ArrayStoreExceptionExample {
public static void main(String[] args) {
// Create an array of integers
// Array of Integer (which is a subtype of Number)
Number[] numberArray = new Integer[5];
try {
// Attempt to store a String into the Number array
numberArray[0] = "This is a string"; // This will cause ArrayStoreException
} catch (ArrayStoreException e) {
// Handle the exception
System.out.println("Error: Cannot store a String in an array of Numbers!");
} finally {
System.out.println("Execution completed.");
}
}
}
Output:
numberArray[0] = "This is a string";
// This will cause ArrayStoreException