Home > Blockchain >  show the subclass exception will be caught java
show the subclass exception will be caught java

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 write 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 just wondered how to write these codes to show the exception handling? 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 to throw ExceptionBeta and ExceptionGamma.

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

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

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

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