ArrayStoreException is a run time exception which is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
Class Hierarchy of ArrayStoreException:
java.lang.Object ↳ java.lang.Throwable ↳ java.lang.Exception ↳ java.lang.RuntimeException ↳ java.lang.ArrayStoreException
Constructors of ArrayStoreException:
-
public ArrayStoreException()
It is used to construct an ArrayStoreException with no message.
-
public ArrayStoreException(String s)
It is used to construct an ArrayStoreException with the specified message.
Example
public class Main { public static void main(String[] args) { Object[] stringArray = new String[4]; stringArray[1] = "w3schools"; //java.lang.ArrayStoreException here stringArray[2] = 20; } } |
Output
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer at Main.main(Main.java:7) |
How to handle with ArrayStoreException?
We can use try, catch or throws keywords to handle ArrayStoreException.
Example
public class Main { public static void main(String[] args) { try{ Object[] stringArray = new String[4]; stringArray[1] = "w3schools"; //java.lang.ArrayStoreException here stringArray[2] = 20; }catch(ArrayStoreException e){ e.printStackTrace(); } } } |
Output
java.lang.ArrayStoreException: java.lang.Integer at Main.main(Main.java:8) |
Java interview questions on Exception Handling
- what is an exception?
- How the exceptions are handled in java?
- What is the difference between error and exception in java?
- Can we keep other statements in between try catch and finally blocks?
- Explain the exception hierarchy in java?
- What are runtime exceptions in java?
- What is outofmemoryerror in java?
- What are checked and unchecked exceptions in java?
- What is the difference between classnotfoundexception and noclassdeffounderror in java?
- Will finally block get executed if return?
- Can we throw an exception without throws?
- What is rethrowing an exception in java?
- What is the use of throws keyword in java?
- What is exception propagation in java?
- Difference between throw and throws in java?
- What is finally in java?
- What is the difference between final finally and finalize in java?
- How to create customized exceptions in java?
- What is classcastexception in java?
- What is stackoverflowerror in java?
- What is the superclass of all exception classes?
- What is the use of printstacktrace method in java?
- What is arraystoreexception in java?