Exceptions: one exception in Stream.map aborts the whole pipeline with no partial result

Streams are not fault tolerant: the moment a mapping function throws, the exception propagates straight out of the terminal operation and any elements already processed are lost, with nothing collected.

Code
List<String> inputs = List.of("1", "2", "oops", "4");
try {
    List<Integer> parsed = inputs.stream().map(Integer::parseInt).toList();
    System.out.println("Unreachable: " + parsed);
} catch (NumberFormatException e) {
    System.out.println("Pipeline aborted: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
Output
Pipeline aborted: NumberFormatException: For input string: "oops"
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