|
|
Re: Java class inheritance and exceptions
|
Posted: Mar 6, 2008 11:52 AM
|
|
I'm not sure I correctly understand what you need, but here's a whack at it.
// divides elements of num array by elements of div array
class ExA {
void divide(int[] num, int[] div) {
for (int i = 0; i < num.length; i++) {
System.out.println(num[i] + "/" +
div[i] + " is " +
num[i]/div[i]);
}
}
}
// extends ExA.divide(), but catches ArithmeticException
class ExB extends ExA {
void divide(int[] num, int[] div) {
try {
super.divide(num, div);
}
catch (ArithmeticException ec) {
System.out.println("Division by zero not allowed");
}
}
}
// extends ExB.divide(), but catches ArrayIndexOutOfBoundsException
class ExC extends ExB {
void divide(int[] num, int[] div) {
try {
super.divide(num, div);
}
catch (ArrayIndexOutOfBoundsException ec) {
System.out.println("Divisor element not found");
}
}
}
class ExceptionTest{
public static void main(String[] args) {
ExA objA = new ExA();
ExA objB = new ExB();
ExA objC = new ExC();
int num[] = {4, 8, 16};
int div[] = {2, 0, 4};
test(objA, num, div, "ExA divide by zero -- expect exception");
test(objB, num, div, "ExB divide by zero -- expect message");
test(objC, num, div, "ExC divide by zero -- expect message");
int num2[] = {4, 8, 16};
int div2[] = {2, 1};
test(objA, num2, div2, "ExA out of bounds -- expect exception");
test(objB, num2, div2, "ExB out of bounds -- expect exception");
test(objC, num2, div2, "ExC out of bounds -- expect message");
}
static void test(ExA divider, int[] num, int[] div, String msg) {
System.out.println("\n" + msg);
try {
divider.divide(num, div);
}
catch (ArithmeticException ec) {
System.out.println(ec);
}
catch (ArrayIndexOutOfBoundsException ec) {
System.out.println(ec);
} //end catch
} //end class
} //end method
This example has three classes, ExA, ExB, and ExC, that implement a method divide(). ExB extends ExA, but adds functionality to catch ArithmeticExceptions in divide(). ExC extends ExB, but adds functionality to catch ArrayIndexOutOfBoundsExceptions.
The ExceptionTest class has a test() method that accepts an ExA object and calls its divide() method with a specified set of numerators and divisors.
We create a set of numerators and divisors that cause an ArithmeticException in ExA, and then call it on an ExA object, an ExB object, and an ExC object to verify that the ExA object throws an exception but subclasses ExB and ExC do not.
Then we do the same thing with arguments that cause an ArrayIndexOutOfBoundsException, this time verifying that the ExC object doesn't throw an exception.
These are the major changes I made to your code:
1. Changed the name of method genException() to divide(), since it won't always throw exceptions.
2. Pass in numerators and divisors as arguments to divide(), so we can vary its behavior to test the conditions we're interested in.
3. Make divide() non-static, so it can be overridden in subclasses.
4. Got rid of unused constructors in ExB and ExC.
5. Created a test method that can be used to test the various objects. Its 'msg' argument just gets printed to the output so we can display a message saying what's being tested and what we expect to happen on each pass.
Here's what the output looks like:
ExA divide by zero -- expect exception 4/2 is 2 java.lang.ArithmeticException: / by zero ExB divide by zero -- expect message 4/2 is 2 Division by zero not allowed ExC divide by zero -- expect message 4/2 is 2 Division by zero not allowed ExA out of bounds -- expect exception 4/2 is 2 8/1 is 8 java.lang.ArrayIndexOutOfBoundsException: 2 ExB out of bounds -- expect exception 4/2 is 2 8/1 is 8 java.lang.ArrayIndexOutOfBoundsException: 2 ExC out of bounds -- expect message 4/2 is 2 8/1 is 8 Divisor element not found
|
|