Why does a Java Lambda which throws a Runtime Exception require brackets?

The Java Language Specification describes the body of a lambda expression

A lambda body is either a single expression or a block (ยง14.2).

This, however,

throw new IllegalArgumentException("fail")

is the throw statement, not an expression. The compiler therefore rejects it as the lambda expression’s body.

You can go down the rabbit hole and learn what all the types of expressions are, here (follow the grammar).

Leave a Comment