Exceptions: a lambda cannot throw a checked exception its interface does not declare

A functional interface's abstract method fixes which checked exceptions an implementing lambda may throw; when it declares none, the lambda body must catch any checked exception itself rather than let it escape.

Code
Runnable task = () -> {
    try {
        throw new java.io.IOException("simulated read failure");
    } catch (java.io.IOException e) {
        System.out.println("Caught inside lambda: " + e.getClass().getSimpleName() + ": " + e.getMessage());
    }
};
task.run();
System.out.println("Runnable completed normally");
Output
Caught inside lambda: IOException: simulated read failure
Runnable completed normally
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms