---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 13 Edition"
java_version: 13
chapter: "Chapter 18. Type Inference"
section: "18.2.5"
heading: "18.2.5. Checked Exception Constraints"
parent: "18.2. Reduction"
source: https://docs.oracle.com/javase/specs/jls/se13/html/jls-18.html#jls-18.2.5
id: "18.2.5"
url: https://jvm.obfuscated.software/jls/se13/18.2.5.html
raw: https://jvm.obfuscated.software/jls/se13/18.2.5.md
---

# 18.2.5. Checked Exception Constraints

A constraint formula of the form ‹*LambdaExpression* →_{throws} T› is reduced as follows:

- If T is not a functional interface type ([§9.8](9.8.md)), the constraint reduces to *false*.

- Otherwise, let the target function type for the lambda expression be determined as specified in [§15.27.3](15.27.3.md). 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.2](18.5.2.2.md).

- Otherwise, if the function type's return type is neither `void` nor 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.2](18.5.2.2.md).

- Otherwise, let E_1, ..., E_n be the types in the function type's `throws` clause 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](11.2.md)). Then there are two cases:

  - If *n* = `0` (the function type's `throws` clause 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 the `throws` clause, 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 the `throws` clause, 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 bound `throws` E_j.

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](9.9.md)), 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](15.13.1.md)) 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 `void` nor a proper type, the constraint reduces to *false*.

- Otherwise, let E_1, ..., E_n be the types in the function type's `throws` clause that are *not* proper types. Let X_1, ..., X_m be the checked exceptions in the `throws` clause of the invocation type of the method reference's compile-time declaration ([§15.13.2](15.13.2.md)) (as derived from the function type's parameter types and return type). Then there are two cases:

  - If *n* = `0` (the function type's `throws` clause 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 the `throws` clause, 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 the `throws` clause, 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 bound `throws` E_j.

Constraints on checked exceptions are handled separately from constraints on return types, because return type compatibility influences applicability of methods ([§18.5.1](18.5.1.md)), while exceptions only influence the invocation type after overload resolution is complete ([§18.5.2](18.5.2.md)). This could be simplified by including exception compatibility in the definition of lambda expression compatibility ([§15.27.3](15.27.3.md)), 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.
