JavaAdvanced#streams#exceptions

What is the difference between checked exception wrapping in streams' lambdas — why can't you throw checked exceptions inside a lambda directly used in a stream?

Functional interfaces used in streams (Function, Consumer, etc.) don't declare checked exceptions in their method signatures, so a lambda implementing them cannot throw a checked exception without wrapping it in a try-catch or an unchecked RuntimeException.

Example
list.stream().map(s -> {
  try { return process(s); } // process() throws IOException
  catch (IOException e) { throw new RuntimeException(e); } // must wrap as unchecked
});

Related Questions

1
JavaAdvanced#collections#oop

What is the difference between the Comparable interface's compareTo() contract and a poorly implemented one?

Open
2
JavaBeginner#jvm#fundamentals

What is JVM?

Open
3
JavaBeginner#jvm#jdk

Difference between JDK, JRE and JVM?

Open