1

For homework, I have to develop an exceptions code:

"Use inheritance to create a superclass of exception called ExceptionA, and two subclasses of the exception called ExceptionB and ExceptionC, where ExceptionB extends ExceptionA, and ExceptionC extends ExceptionB. Develop a program to prove that the catch block for the type ExceptionA catches exceptions of ExceptionB and ExceptionC".

I do not understand how to do this.

john_science
  • 6,325
  • 6
  • 43
  • 60
OHHH
  • 1,011
  • 3
  • 16
  • 34
  • 1
    Well, it's impossible to create a superclass of `Exception` so that should a subclass. Appart from that, http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html – madth3 Oct 09 '12 at 01:12

3 Answers3

4

Pretend that this is a family of asexually reproducing things called Exceptions.

Family Tree

Exception C is the son/daughter (what do I even call them) of Exception B, who is the son/daughter of Exception A, who is the son/daughter of Exception. Exception C inherited genes from Exception B, who inherited genes from Exception A, etc.

What your teacher wants you to do is show that you can "catch" Exception C's using the same method to that you are using to "catch" Exception A's. So you would be using try/catch statements and throw statements (to generate the exception).

Clark
  • 1,357
  • 1
  • 7
  • 18
3

Here is a very simple code. You can further enhance for your learning.

Create exception ExceptionA and define require constructors and methods:

    public class ExceptionA extends Exception {

        public ExceptionA(String message){
            super(message);
        }
    }

Create exception ExceptionB and define require constructors and methods:

    public class ExceptionB extends ExceptionA {

        public ExceptionB(String message){
            super(message);
        }
    }

Create exception ExceptionC and define require constructors and methods:

    public class ExceptionC extends ExceptionB {

        public ExceptionC(String message){
            super(message);
        }
    }

Create TestException class which catches ExceptionB and ExceptionC using ExceptionA as below:

    public class TestException {

        public static void main(String[] args){

            try{
                getExceptionB();
            }catch(ExceptionA ea){
                ea.printStackTrace();
            }

            try{
                getExceptionC();
            }catch(ExceptionA ea){
                ea.printStackTrace();
            }

        }

        public static void  getExceptionB() throws ExceptionB{
            throw new ExceptionB("Exception B");
        }

        public static void  getExceptionC() throws ExceptionC{
            throw new ExceptionC("Exception C");
        }

    }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • I love you, got it now, I did get the inheritance thing before I created this post, but you cleared my doubts about the implementation of the exceptions. – OHHH Oct 09 '12 at 01:50
0

I expect it is: Use inheritance to create a subclass of Exception called ExceptionA.

So, to help you.

Exception -> ExceptionA -> ExceptionB -> ExceptionC

So, if I want to create a subclass of type Object then: public class ObjectA extends Object { // put new methods in here }

So, try creating your subclasses, then show some code if you get stuck.

This may help you, but please don't copy the answer as your assignment:

How to define custom exception class in Java, the easiest way?

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166