
Object x = new Integer(0); System.out.println((String)x);
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 | NegativeArraySizeException | ArrayIndexOutOfBoundsException | ClassCastException | ArrayStoreException ex) {
System.out.println("\n Exception Generated: "
+ ex.getMessage());
ex.printStackTrace();
}
}
}
}
OUTPUT:
Labels: Core JAVA