Different Exception Generate in Array 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

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.

4 - 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.

5 - 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.

6 - 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

Object x = new Integer(0);
System.out.println((String)x);
 

7 - 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:

Object x[] = new String[7];
x[0] = new Integer(0);

Example

public class ExceptionInArray {

 

      public static void main(String[] args) {

            int array[] = null;

            int arraySize = 4;

            int arrayInc = -0;

            int output;

 

            for (int i = 0; i < 6; i++) {

                  try {

                        // Multiple Switch Conditions

                        switch (i) {

                        case 0:

                              output = array[0]; // Generates a NullPointerException.

                              break;

                        case 1:

                              array = new int[arrayInc]; // Generates a

                              // NegativeArraySizeException.

                              output = array[arrayInc];

                              break;

                        case 2:

                              array = new int[arraySize]; // Generate the

                              // ArrayIndexOutOfBoundsException.

                              output = array[arraySize];

                              break;

                        case 3:

                              array = new int[5]; // Generate the

                              // IndexOutOfBoundsException.

                              output = array[5];

                        case 4:

                              Object newArray = new Integer(0); // Generate the

                              // ClassCastException.

                              System.out.println((String) newArray);

                        case 5:

                              Object X[] = new String[-5]; // Generate the

                              // ArrayStoreException.

                              X[0] = new Integer(0);

                              System.out.println(X);

                        }

 

                  } catch (NullPointerException ex1) {

                        System.out.println("\n Exception Generated: "

                                    + ex1.getMessage());

                        ex1.printStackTrace();

 

                  } catch (NegativeArraySizeException ex2) {

                        System.out.println("\n Exception Generated: "

                                    + ex2.getMessage());

                        ex2.printStackTrace();

 

                  } catch (ArrayIndexOutOfBoundsException ex3) {

                        System.out.println("\n Exception Generated: "

                                    + ex3.getMessage());

                        ex3.printStackTrace();

 

                  } catch (IndexOutOfBoundsException ex4) {

                        System.out.println("\n Exception Generated: "

                                    + ex4.getMessage());

                        ex4.printStackTrace();

 

                  } catch (ClassCastException ex5) {

                        System.out.println("\n Exception Generated: "

                                    + ex5.getMessage());

                        ex5.printStackTrace();

 

                  } catch (ArrayStoreException ex6) {

                        System.out.println("\n Exception Generated: "

                                    + ex6.getMessage());

                        ex6.printStackTrace();

                  }

            }

      }

}

OUTPUT

 cmdoutputException.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all