samedi 4 juin 2022

How to intercept super class method invocation to handle exception

I have a class with methods that don't handle exceptions. I want to be able to call any of those methods and if they throw exception - handle it properly and return error response.

For example I have super class SuperClass:

public class SuperClass {
    public Object method1() throws Exception {...}
    public Object method2() throws Exception {...}
}

And when I call any of its methods instead of throwing exception I want them to return correct error (without modifying those methods).

Maybe I need to create subclass and somehow call the proper method from super class and handle exception?

public class SubClass extends SuperClass {
    public Object callSuperClassMethodAndHandleException() {...}
}

Aucun commentaire:

Enregistrer un commentaire