I've seen couple of examples of this but unfortunately none where the method throws an exception.
To be clear, there is one generic method I have that receives another method as reference.
This method throws an exception on its method's signature, when this method (returnFullName in the example) does not throw an Exception no problem for the compiler, but when it does the compiler complains "Unhandled exception".
I still cannot figure out how to solve this, is there any idea how to handle the exceptions in these cases?
public class MyClass{
private static String returnFullName(String name) throws Exception{
return "full name " + name;
}
public static String calculateByName(String name) {
try {
myGenericMethod("John Doe", RemoteFileUtils::returnFullName);
} catch (Exception e) {
return "fallback name";
}
}
private static <T> T myGenericMethod(final String name, final Function<String, T> call) {
//do stuff
return call.apply(name);
}
}