by Dinesh Thakur Category: Exception Handling

ArrayStoreException is thrown when ever we storing something in an array. If we store wrong type object into an array then this will be thrown. Here is the Java example which explains this.

import java.io.*;

  class ArrayStoreExceptionJavaExample
  {
         public static void main(String aa[])  throws IOException
          {
                  BufferedReader BF= new BufferedReader(new InputStreamReader(System.in));
                  String Str;
                  int arr[]=new int[5];
                  int i,x;
                  System.out.println("Enter Five Number : ");
                  for(i=0;i<=4;i++)
                        {
                              Str=BF.readLine();
                              try
                               {
                                   arr[i]=Integer.parseInt(Str);
                               }
                                catch(NumberFormatException e)
                                        {
                                            System.out.println("Wrong data type enterd, try again");
                                            i--;
                                            continue;
                                        }
                       }
                                           System.out.println("Number Entered are :");
                                           x=0;
                                           while(true)
                                                    {
                                                      try
                                                            {
                                                              System.out.println(arr[x]);
                                                              x++;
                                                            }
                                                               catch(ArrayIndexOutOfBoundsException e1)
                                                                       {
                                                                           System.out.println("Number are Over:");
                                                                           return;
                                                                       }
                                                     }
           }
  }      

ArrayStoreException



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.