Array Exceptions in Java

In this article we are going to describe the many exceptions that are possibly generated by an array in Java. So now we describe each Exception type in detail one by one. First we give you a little introduction to arrays in Java.

Introduction

 
In this article, we are going to describe the many exceptions that are possibly generated by an array in Java. So now we describe each Exception type in detail one by one. First, we give you a little introduction to arrays in Java.
 

Array Definition

 
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. And it also contains similar types of data. Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, the numbering begins with 0.
 
objects-tenElementArray.gif 
 

Possible Error Type in Array

 
1. NullPointerException - In Java a NullPointerException is a class which also extends RuntimeException. There are the following conditions where the NullPointerException is generated. They are thrown when an application attempts to use null in a case where an object is required.
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
 
2. NegativeArraySizeException - This error is thrown when anyone wants create an array with a negative size.
 
NegativeArraySizeException is a class in Java which extends RuntimeException.
 
3. ArrayIndexOutOfBoundsException - This type of error is generated when an array has been accessed with an illegal index. In other words when an array is accessed by a negative index or more than the size of the array. In Java it's a separate class and this class extends the IndexOutOfBoundException class.
 
4. IndexOutOfBoundsException - This type of exception is thrown by all indexing pattern data types such as an array string and a vector etc. when it is accessed out of the index (range). IndexOutOfBoundException is also a separate class in Java and it extends RuntimeException.
 
5. ClassCastException - The ClassCastException is thrown when the following code has attempted to cast an object to a subclass of which it is not an instance. ClassCastException is also a separate class in Java and it extends RuntimeException.
ClassCastException condition
  1. Object x = new Integer(0);  
  2. System.out.println((String)x);   
6. ArrayStoreException - This type of exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException. ArrayStoreException is also a separate class in Java and it extends RuntimeException.
 
For example:
  1. Object x[] = new String[7];  
  2. x[0] = new Integer(0); 
Example
  1. public class ExceptionInArray {  
  2.     public static void main(String[] args) {  
  3.         int array[] = null;  
  4.         int arraySize = 4;  
  5.         int arrayInc = -0;  
  6.         int output;  
  7.         for (int i = 0; i < 6; i++) {  
  8.             try {  
  9.                 // Multiple Switch Conditions  
  10.                 switch (i) {  
  11.                     case 0:  
  12.                         output = array[0]; // Generates a NullPointerException.  
  13.                         break;  
  14.                     case 1:  
  15.                         array = new int[arrayInc]; // Generates a  
  16.                         // NegativeArraySizeException.  
  17.                         output = array[arrayInc];  
  18.                         break;  
  19.                     case 2:  
  20.                         array = new int[arraySize]; // Generate the  
  21.                         // ArrayIndexOutOfBoundsException.  
  22.                         output = array[arraySize];  
  23.                         break;  
  24.                     case 3:  
  25.                         array = new int[5]; // Generate the  
  26.                         // IndexOutOfBoundsException.  
  27.                         output = array[5];  
  28.                     case 4:  
  29.                         Object newArray = new Integer(0); // Generate the  
  30.                         // ClassCastException.  
  31.                         System.out.println((String) newArray);  
  32.                     case 5:  
  33.                         Object X[] = new String[-5]; // Generate the  
  34.                         // ArrayStoreException.  
  35.                         X[0] = new Integer(0);  
  36.                         System.out.println(X);  
  37.                 }  
  38.             } catch (NullPointerException ex1) {  
  39.                 System.out.println("\n Exception Generated: "  
  40.                     +  
  41.                     ex1.getMessage());  
  42.                 ex1.printStackTrace();  
  43.             } catch (NegativeArraySizeException ex2) {  
  44.                 System.out.println("\n Exception Generated: "  
  45.                     +  
  46.                     ex2.getMessage());  
  47.                 ex2.printStackTrace();  
  48.             } catch (ArrayIndexOutOfBoundsException ex3) {  
  49.                 System.out.println("\n Exception Generated: "  
  50.                     +  
  51.                     ex3.getMessage());  
  52.                 ex3.printStackTrace();  
  53.             } catch (IndexOutOfBoundsException ex4) {  
  54.                 System.out.println("\n Exception Generated: "  
  55.                     +  
  56.                     ex4.getMessage());  
  57.                 ex4.printStackTrace();  
  58.             } catch (ClassCastException ex5) {  
  59.                 System.out.println("\n Exception Generated: "  
  60.                     +  
  61.                     ex5.getMessage());  
  62.                 ex5.printStackTrace();  
  63.             } catch (ArrayStoreException ex6) {  
  64.                 System.out.println("\n Exception Generated: "  
  65.                     +  
  66.                     ex6.getMessage());  
  67.                 ex6.printStackTrace();  
  68.             }  
  69.         }  
  70.     }  
  71. }  
OUTPUT
 
 Array Exception in Java