In a project I am working at, I have found a class which wraps all methods of its super-class in some elaborate exception handling. It looks similar to that:

public void method1() throws ExceptionA {
    String exceptionString = "";
    try {
        super.method1();
    } catch (ExceptionA e) {
         exceptionString = // <convert the exception to string in elaborate way>
         throw e;
    } finally {
         // <an elaborate logger call which uses value of exceptionString>
    }
}

public void method2() throws ExceptionB, ExceptionC {
    String exceptionString = "";
    try {
        super.method2();
    } catch (ExceptionB | ExceptionC e) {
         exceptionString = // <convert the exception to string in elaborate way>
         throw e;
    } finally {
         // <an elaborate logger call which uses value of exceptionString>
    }
}

// ... <a bunch of other methods like this>

I immediately though "Wow, how could would it be to have one generic wrapper and just call it in every of these methods. The class would be like 10x shorter!". So I got to work.

This is where I got stuck:

private static interface <E extends Exception> ThrowingMethod {
    void run() throws E;
}

public <E extends Exception> void wrapMethod(ThrowingMethod<E> method) throws E {
    String exceptionString = "";
    try {
        method.run();
    } catch (Exception e) {
         exceptionString = // <convert the exception to string in elaborate way>
         throw e;
    } finally {
         // <an elaborate logger call which uses value of exceptionString>
    }
}

public void method1() throws ExceptionA {
    wrapMethod(super::method1); // works
}

public void method2() throws ExceptionB, ExceptionC {
    wrapMethod(super::method2); // Error in Eclipse: "Unhandled exception type Exception"
}

// ... <a bunch of other methods like this>

In conclusion, this approach works for methods that throws only one type of checked exception. When method throws multiple checked exceptions, Java assumes that the exception type is Exception.

I tried to add more generic parameters to ThrowingMethod and wrapMethod but it doesn't change anything.

How can I get a functional interface to work with multiple generic exceptions?

  • I'd say one of the problems would be that your wrapMethod() would need to support multiple exceptions in its throws-clause in order to call it like in method2() and have that throw ExceptionB and/or ExceptionC. So you might need to provide overloads for more than one exception (probably not that many since IMO a method with 20 exceptions in its throws-clause is bad design anyways ). Since you'd need multiple wrapMethod()'s you'd probably need to extract the conversion and logging parts into separate methods - if possible. – Thomas 5 mins ago
  • @Thomas I tried method with 5 generic exceptions instead of one. It hasn't changed a thing. Wrapping method1 still works and wrapping method2 still defaults to Exception. So it won't work, unless you are suggesting that I should make a separate wrapper for each number of exceptions but this would defeat the point of this exercise which is the make the code shorter. – NO_NAME 38 secs ago

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.