The 30 second answer
Unchecked exceptions from a lambda propagate out of the terminal operation and abort the pipeline. Checked exceptions cannot be declared on Function or Predicate, so I wrap them as unchecked, map failures into a result type, or leave the stream and use a loop when per-element error handling is the real logic. Close resource streams with try-with-resources.
- Unchecked exceptions thrown in a lambda propagate out of the terminal operation and abort the pipeline.
- Checked exceptions cannot be thrown from
Function,Predicate, and similar types because their functional methods declare nothrowsclause. - [Opinion] Options: wrap in a helper that catches and rethrows as unchecked (
UncheckedIOExceptionforIOException); map failures into a result type (for exampleOptionalor anEither-style record) and filter later; or move the throwing code out of the stream into a loop when per-element error handling is the core logic. - In parallel streams an exception from one task is rethrown in the caller; other tasks may continue briefly. [Opinion / Impl — not specified in detail by the Javadoc]
- Streams on I/O resources (
Files.lines) must be closed: use try-with-resources;BaseStreamisAutoCloseable.
Readability tip: [Opinion] Prefer streams for declarative transforms and aggregations; prefer loops for complex control flow, early exits with multiple states, checked exceptions, or hot paths where profiling shows overhead.
Sources:
Sources (0)
See inline citations in the answer above.