An exception interrupts ordinary execution when an operation cannot complete normally. Decide early how failure will be made explicit, whether recovery is possible, and how resources will be released. Success may be silent; failure should not be. section 2.5 section 7.0
In a functional pipeline, an exception disrupts the expected input-to-output flow. Java also requires a checked IOException from file reading to be handled; wrapping the stream in an outer try-catch does not resolve the compiler complaint inside the pipeline. section 6.3.2
List<ReadResult> contents = filenames.stream()
.map(filename -> {
try {
List<String> content = Files.readAllLines(Path.of(filename));
return new ReadResult(Optional.of(content), #1
Optional.empty()); #1
} catch (IOException e) {
return new ReadResult(Optional.empty(), #2
Optional.of(e)); #2
}
})
.toList();
Catch the file-reading exception at the operation and turn each outcome into a value: content on success or an error on failure. This makes failure available to the caller instead of interrupting the pipeline. section 6.3.2
Alternatively, handle the failure inside the pipeline, for example by returning a default value. If a result wrapper makes the pipeline noisy, extract the file-reading operation behind a clear name; a loop with a local catch can also be a practical choice. section 6.3.2
Choose handling according to the failure and the boundary:
- Use a domain-specific exception or an explicit result type for an anticipated business failure, so the caller can respond appropriately. section 7.1.1
- Translate low-level failures before they cross architectural boundaries, but preserve the original cause for diagnosis. section 7.3
- Keep protected blocks narrow, avoid generic catches without justification, and log useful context without sensitive data. section 7.3
- Do not expose raw stack traces to users; provide a controlled response that does not reveal sensitive technical details. section 7.1.1

























