The Artima Developer Community
Sponsored Link

Java Answers Forum
Java class inheritance and exceptions

4 replies on 1 page. Most recent reply: Mar 6, 2008 12:29 PM by Denise B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Denise B

Posts: 5
Nickname: elusive57
Registered: Feb, 2008

Java class inheritance and exceptions Posted: Mar 5, 2008 5:27 PM
Reply to this message Reply
Advertisement
Trying to show inheritance and exceptions. Code seems to work all the way down to line to print error messages. My code is probably whacky and missing some key element, but I've worked on this for many hours trying different variations and can't seem to come up with the right one. Any suggestions would be greatly appreciated. Thanks!

class ExA{
static void genException(){

int num[]= {4,8,16,32,64,128,256};
int div[]= {2,0,4,4,0,8};

for(int i=0; i<num.length; i++){
System.out.println(num[i] + "/" +
div[i] + "is" +
num[i]/div[i]);
}
}
}
class ExB extends ExA{
static void ExB(String msg1){
msg1 = ("Division by zero not allowed");
}
}
class ExC extends ExB{
static void ExC(String msg2){
msg2 = ("Divisor element not found");
}
}
class ExceptionTest{
public static void main(String[] args){
ExC err = new ExC();

try {
ExA.genException();
}
catch (ArithmeticException ec){
System.out.println(err.msg1 + "");
}
catch (ArrayIndexOutOfBoundsException ec){
System.out.println(err.msg2 + "");
}//end catch
}//end class
}//end method


George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Java class inheritance and exceptions Posted: Mar 6, 2008 7:10 AM
Reply to this message Reply
I had to change the print statements in the catch blocks a bit to get your example to compile. Here's the modified version:
class ExA {
  static void genException() {
 
    int num[] = {4, 8, 16, 32, 64, 128, 256};
    int div[] = {2, 0, 4, 4, 0, 8};
  
    for (int i = 0; i < num.length; i++) {
      System.out.println(num[i] + "/" +
                         div[i] + " is " +
                         num[i]/div[i]);
    }
  }
}
 
class ExB extends ExA {
  static void ExB(String msg1) {
    msg1 = ("Division by zero not allowed");
  }
}
 
class ExC extends ExB {
  static void ExC(String msg2) {
    msg2 = ("Divisor element not found");
  }
}
 
class ExceptionTest{
  public static void main(String[] args){
 
    try {
      ExA.genException();
    }
    catch (ArithmeticException ec) {
      System.out.println(ec);
    }
    catch (ArrayIndexOutOfBoundsException ec) {
      System.out.println(ec);
    } //end catch
  } //end class
} //end method

This doesn't use classes ExB or ExC. I'm not sure what you're trying to accomplish with those two classes.

When I run this version I get:

4/2 is 2
java.lang.ArithmeticException: / by zero

Can you explain a bit what you want to demonstrate with your program?

Denise B

Posts: 5
Nickname: elusive57
Registered: Feb, 2008

Re: Java class inheritance and exceptions Posted: Mar 6, 2008 8:44 AM
Reply to this message Reply
Thank you, George, for taking a look at my code....I am trying to show that ExB inherits from ExA and ExC inherits from ExB. I am also supposed to show that ExA, which is a superclass, catches exceptions from ExB and ExC....maybe it is too much for one file, i'm not sure...as you can tell, i am truly stuggling in this Java class!

George Berger

Posts: 24
Nickname: gcb
Registered: Jul, 2007

Re: Java class inheritance and exceptions Posted: Mar 6, 2008 11:52 AM
Reply to this message Reply
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

Denise B

Posts: 5
Nickname: elusive57
Registered: Feb, 2008

Re: Java class inheritance and exceptions Posted: Mar 6, 2008 12:29 PM
Reply to this message Reply
Thank you so much, George, for such a detailed code and explanation. It is much more than I expected and I am extremely grateful. It will take me a minute or two to wrap my brain around this so I will be able to come up with something similar on my own when faced with this again. Thank you!!

Flat View: This topic has 4 replies on 1 page
Topic: Java class inheritance and exceptions Previous Topic   Next Topic Topic: Set Text

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use