Java 8 lambda: automatically throwing exception RuntimeException by default?

I noticed something that seems reasonable, Eclipse JDT, but it seems to be nowhere defined:

<!-- 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 a method with a lambda as a PredicateEx step, an undefined TException is considered a RuntimeException if the lambda does not throw anything. I found a piece of code in the Eclipse JDT that did this, but is this a clear output behavior like lambda or just some kind of decision made in a compiler implementation? Since the default exception may also be the default exception instead (= the upper bound of the TException), and I am very concerned that I am going to rewrite all the methods of handling functions in such a way as to correctly handle checked exceptions.

Examples of callers:

<!-- language: lang-java -->

iterateEx(listOfResultSet, rs -> true); // throws RuntimeException, no try-catch required
iterateEx(listOfResultSet, rs -> rs.getBoolean("SOME_COLUMN")); // throws SQLException

PredicateEx - Predicate, :

<!-- language: lang-java -->

@FunctionalInterface
public interface PredicateEx<T, TException extends Exception>
{
    boolean testEx(T t) throws TException;
}
+4
1

18 Java. throws 18.2.5. , throw . , ,

E1, ..., En - , . - , . , poly result, . X1, ..., Xm - , - (§11.2). :

  • ...

  • n > 0, : i (1 ≤ i ≤ m), Xi throws, , j (1 ≤ j ≤ n), ‹Xi <: Ej›. , j (1 ≤ j ≤ n) throws Ej.

? , throws (.. , ) , -. , ? JLS :

, , , . , , , . , .

, . , JLS .

18.4 , "" . ( , ):

  • αi , L1, ..., Lk, Ti = lub(L1, ..., Lk) (§4.10.4).
  • , αi, αi - Exception, Throwable Object, Ti = RuntimeException.
  • , αi U1, ..., Uk, Ti = glb(U1, ..., Uk) (. 5.1.10).

! , , throws RuntimeException, "" .

+1

All Articles