Home > OS >  How to prove that a Catch block for the Parent-exception will handle subclasses as well
How to prove that a Catch block for the Parent-exception will handle subclasses as well

Time:03-27

I have met this problem.

I have created a class CatHandler, with 3 inner classes (ExceptionAlpha extends Exception, ExceptionBeta extends ExceptionAlpha, ExceptionGammer extends ExceptionBeta). These three exception subclasses are empty; they contain no code. All code should be written in CatHandler.

Now the question is to write some code in CatHandler to show that ExceptionBeta and ExceptionGammer will be caught in the catch block of type ExceptionAlpha?

For the output, we can use System.err.println(), getMessage() and printStackTrace() and other appropriate print statements to show that the exception subclasses have been successfully caught.

I'm wondered how to show that exception handling happens in such a way? It is really confusing.

public class CatHandler {

    class ExceptionAlpha extends Exception {

    }

    class ExceptionBeta extends ExceptionAlpha {

    }


    class ExceptionGammer extends ExceptionBeta {

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

CodePudding user response:

the question is to write some code in CatHandler to show that ExceptionBeta and ExceptionGammer will be caught in the catch block of type ExceptionAlpha.

First, you need to declare a few methods that will throw ExceptionBeta and ExceptionGamma.

Because both of them are checked exceptions, you should include a throws clause in the method declarations.

It's better to define all the nested classes as static, otherwise these exceptions will always require an object enclosing class (i.e. CatHandler) in order to be instantiated.

The code in the main() invokes the unsafe behavior and handless it with catch blocks expecting ExceptionAlpha or its subtypes.

To demonstrate the actual type of a caught exception, we can extract the class name from its Class object or print stack trace (class name will mentioned at the very beginning a stack trace). Both options are shown below.

public class CatHandler {

    public void m1() throws ExceptionBeta {
        throw new ExceptionBeta();
    }

    public void m2() throws ExceptionGamma {
        throw new ExceptionGamma();
    }

    public static class ExceptionAlpha extends Exception{}
    public static class ExceptionBeta extends ExceptionAlpha {}
    public static class ExceptionGamma extends ExceptionBeta {}

    public static void main(String[] args) {
        CatHandler catHandler = new CatHandler();
        try {
            catHandler.m1();
        } catch (ExceptionAlpha e) {
            System.out.println("Class name: "   e.getClass().getSimpleName());
            e.printStackTrace();
        }

        try {
            catHandler.m2();
        } catch (ExceptionAlpha e) {
            System.out.println("Class name: "   e.getClass().getSimpleName());
            e.printStackTrace();
        }
    }
}

Output

Class name: ExceptionBeta
Class name: ExceptionGamma

_ path _.CatHandler$ExceptionBeta
    at _ path _.CatHandler.m1(CatHandler.java:6)
    at _ path _.CatHandler.main(CatHandler.java:36)

_ path _.CatHandler$ExceptionGamma
    at _ path _.CatHandler.m2(CatHandler.java:10)
    at _ path _.CatHandler.main(CatHandler.java:42)

CodePudding user response:

Override getMessage in each exception to return something different. Polymorphism will allow ExceptionAlpha to catch it's subclasses and then use the specific implementations of the respective ones.

  • Related