Java Reference
In-Depth Information
public static void main(String[] args) {
// Code for main()..
}
// Divide method
public static int divide(int[] array, int index) {
// Code for divide()...
}
}
The idea behind the divide() method is to pass it an array and an index as arguments. By choosing the
values in the array and the index value judiciously, we can get ArithmeticException and
ArrayIndexOutOfBoundsException exceptions thrown. We'll need a try block plus two catch blocks
for the exceptions, and we will throw in a finally block for good measure. Here's the code for divide() :
public static int divide(int[] array, int index) {
try {
System.out.println("\nFirst try block in divide() entered");
array[index + 2] = array[index]/array[index + 1];
System.out.println("Code at end of first try block in divide()");
return array[index + 2];
} catch(ArithmeticException e) {
System.out.println("Arithmetic exception caught in divide()");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index-out-of-bounds exception caught in divide()");
} finally {
System.out.println("finally block in divide()");
}
System.out.println("Executing code after try block in divide()");
return array[index + 2];
}
We can define the main() method with the following code:
public static void main(String[] args) {
int[] x = {10, 5, 0}; // Array of three integers
// This block only throws an exception if method divide() does
try {
System.out.println("First try block in main() entered");
System.out.println("result = " + divide(x,0)); // No error
x[1] = 0; // Will cause a divide by zero
System.out.println("result = " + divide(x,0)); // Arithmetic error
x[1] = 1; // Reset to prevent divide by zero
System.out.println("result = " + divide(x,1)); // Index error
} catch(ArithmeticException e) {
System.out.println("Arithmetic exception caught in main()");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index-out-of-bounds exception caught in main()");
Search WWH ::




Custom Search