A constraint formula of the form ‹LambdaExpression →_{throws} T› is reduced as follows:
-
If T is not a functional interface type (§9.8), the constraint reduces to false.
-
Otherwise, let the target function type for the lambda expression be determined as specified in §15.27.3. If no valid function type can be found, the constraint reduces to false.
-
Otherwise, if the lambda expression is implicitly typed, and one or more of the function type's parameter types is not a proper type, the constraint reduces to false.
This condition never arises in practice, due to the substitution applied to the target type in §18.5.2.
-
Otherwise, if the function type's return type is neither
voidnor a proper type, the constraint reduces to false.This condition never arises in practice, due to the substitution applied to the target type in §18.5.2.
-
Otherwise, let E_1, ..., E_n be the types in the function type's
throwsclause that are not proper types. If the lambda expression is implicitly typed, let its parameter types be the function type's parameter types. If the lambda body is a poly expression or a block containing a poly result expression, let the targeted return type be the function type's return type. Let X_1, ..., X_m be the checked exception types that the lambda body can throw (§11.2). Then there are two cases:- If n =
0(the function type'sthrowsclause consists only of proper types), then if there exists some i (1 ≤ i ≤ m) such that X_i is not a subtype of any proper type in thethrowsclause, the constraint reduces to false; otherwise, the constraint reduces to true. - If n >
0, the constraint reduces to a set of subtyping constraints: for all i (1 ≤ i ≤ m), if X_i is not a subtype of any proper type in thethrowsclause, then the constraints include, for all j (1 ≤ j ≤ n), ‹X_i<:E_j›. In addition, for all j (1 ≤ j ≤ n), the constraint reduces to the boundthrowsE_j.
- If n =
A constraint formula of the form ‹MethodReference →_{throws} T› is reduced as follows:
-
If T is not a functional interface type, or if T is a functional interface type but does not have a function type (§9.9), the constraint reduces to false.
-
Otherwise, let the target function type for the method reference expression be the function type of T. If the method reference is inexact (§15.13.1) and one or more of the function type's parameter types is not a proper type, the constraint reduces to false.
-
Otherwise, if the method reference is inexact and the function type's result is neither
voidnor a proper type, the constraint reduces to false. -
Otherwise, let E_1, ..., E_n be the types in the function type's
throwsclause that are not proper types. Let X_1, ..., X_m be the checked exceptions in thethrowsclause of the invocation type of the method reference's compile-time declaration (§15.13.2) (as derived from the function type's parameter types and return type). Then there are two cases:- If n =
0(the function type'sthrowsclause consists only of proper types), then if there exists some i (1 ≤ i ≤ m) such that X_i is not a subtype of any proper type in thethrowsclause, the constraint reduces to false; otherwise, the constraint reduces to true. - If n >
0, the constraint reduces to a set of subtyping constraints: for all i (1 ≤ i ≤ m), if X_i is not a subtype of any proper type in thethrowsclause, then the constraints include, for all j (1 ≤ j ≤ n), ‹X_i<:E_j›. In addition, for all j (1 ≤ j ≤ n), the constraint reduces to the boundthrowsE_j.
- If n =
Constraints on checked exceptions are handled separately from constraints on return types, because return type compatibility influences applicability of methods (§18.5.1), while exceptions only influence the invocation type after overload resolution is complete (§18.5.2). This could be simplified by including exception compatibility in the definition of lambda expression compatibility (§15.27.3), but this would lead to possibly surprising cases in which exceptions that can be thrown by an explicitly typed lambda body change overload resolution.
The exceptions thrown by a lambda body cannot be determined until i) the parameter types of the lambda are known, and ii) the target type of result expressions in the body is known. (The second requirement is to account for generic method invocations in which, for example, the same type parameter appears in the return type and the throws clause.) Hence, we require both of these, as derived from the target type T, to be proper types.
One consequence is that lambda expressions returned from other lambda expressions cannot generate constraints from their thrown exceptions. These constraints can only be generated from top-level lambda expressions.
Note that the handling of the case in which more than one inference variable appears in a function type's throws clause is not completeness-preserving. Either variable may, on its own, satisfy the constraint that each checked exception be declared, but we cannot be sure which one is intended. So, for predictability, we constrain them both.