I noticed something seemly reasonable by Eclipse JDT but doesn't seem to be defined anywhere:
<!-- language: lang-java -->
public static <T, TException extends Exception> void iterateEx(
Iterable<T> iterable, PredicateEx<T, TException> step) throws TException
{
for (T item : iterable)
{
if (step.testEx(item))
{
ThreadExt.yield(); // sleep 0.001s
}
}
}
When I call the method with a lambda as the PredicateEx step, the unspecified TException is assumed to be RuntimeException if the lambda throws nothing. I found the piece of code in Eclipse JDT doing that but is it an well-defined behavior from lambda type inference, or just some decision made in the compiler implementation? Because it might also be possible for the default exception to be Exception instead (= upper bound of TException), and I'm quite concerned because I'm about to rewrite all function-taking methods that way to handle checked exceptions correctly.
Callers' examples are like:
<!-- language: lang-java -->
iterateEx(listOfResultSet, rs -> true); // throws RuntimeException, no try-catch required
iterateEx(listOfResultSet, rs -> rs.getBoolean("SOME_COLUMN")); // throws SQLException
The PredicateEx is an variant of Predicate that allows throwing of exception:
<!-- language: lang-java -->
@FunctionalInterface
public interface PredicateEx<T, TException extends Exception>
{
boolean testEx(T t) throws TException;
}