Unfortunately, Java 8 did not account for creating lambda expressions which may throw an Exception. This shortcoming has caused a great deal of frustration when developing an exceptional-codebase which should be expressed functionally rather than with object orientation.
This repository aims to help rememdy the deficiencies of Java 8 functional programming.
What can I do with these interfaces that I cannot do with Java 8's built-in FunctionalInterface?
Users of this repository, can express code that explicitly throws Exception, without ever explicitly handling it by defering the try/catch to the respective FunctionalInterface's static tryInvoke method.
publicstaticvoidmain(String[] args) {
ExceptionalBiFunction<Object, Object, Object[]> testFunction = (name, age) -> {
booleanvalidName = nameinstanceofString;
booleanvalidAge = ageinstanceofInteger;
booleanvalidPerson = validName && validAge;
// throws potential exceptionif(!validPerson) {
// This line is impossible to declare within a java.util.BiFunctionthrownewThrowable();
}
Object[] person = newObject[2];
person[1] = age;
person[0] = name;
returnperson;
};
StringmyName= "Leon";
IntegermyAge = 24;
Stringerror = "Failed to call function with arguments `%s` and `%s`";
StringoptionalErrorMessage = String.format(error, myName, myAge);
ExceptionalBiFunction.tryInvoke(testFunction, myName, myAge, optionalErrorMessage);
}
This dependency also includes a fallback feature, which allows you to pass a fallback method in the case of an exception.