Different Exception Generate in Array.
| Previous | Home | Next |
Following program for Generate the different Exception in array( using multiple switch and catch).
/*
* Save as a ExceptionGenerate.java
* Program for Generates the Different Execptions(Array)
*/
package r4r.co.in;
public class ExceptionGenerate {
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();
}
}
}
}
java.lang.NullPointerException
Exception Generated: nullat r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:20)
Exception Generated: 0
java.lang.ArrayIndexOutOfBoundsException: 0Exception Generated: 4
at r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:24)java.lang.ArrayIndexOutOfBoundsException: 4
Exception Generated: 5Exception Generated: java.lang.Integer cannot be cast to java.lang.String
at r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:28)java.lang.ArrayIndexOutOfBoundsException: 5
Exception Generated: null
at r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:32)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:35)
java.lang.NegativeArraySizeException
at r4r.co.in.ExceptionGenerate.main(ExceptionGenerate.java:37)
| Previous | Home | Next |