Can a Lambda expression throw an exception? How can you handle exceptions in a Lambda?

Lambda expressions can throw exceptions, but the functional interface's abstract method signature governs which checked exceptions are allowed. If the interface doesn't declare a checked exception, the lambda must handle it internally with try-catch or wrap it as an unchecked exception.

Key Points

Example

A lambda passed to Stream.map() that calls a method throwing IOException must catch the IOException inside the lambda body, since Function.apply() doesn't declare it.

List<String> paths = getPaths();

List<String> contents = paths.stream()
        .map(path -> {
            try {
                return Files.readString(Path.of(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        })
        .collect(Collectors.toList());

Interview Tip

A concise interview answer is

Lambdas can throw exceptions, but only what the functional interface's method signature allows. Since interfaces like Function or Consumer don't declare checked exceptions, I have to catch a checked exception inside the lambda, typically wrapping it in an unchecked exception so it can still propagate out of the stream pipeline.

Last updated: 16 July 2026

Previous question: How can a Lambda expression access variables outside its scope? What is the concept behind it?

Next question: What is the difference between Optional.of() and Optional.ofNullable()?

More Java 8 interview questions

All Java 8 questions