2

Why does the following code,

CompletionStage<Optional<String>> future = completedFuture(Optional.empty());
future.thenApply(optStr -> {
  return optStr.map(s -> s.length())
      .orElseThrow(() -> new IllegalArgumentException("Empty string"));
});

fail to compile with,

unreported exception X; must be caught or declared to be thrown

Passing an explicit type argument of IllegalArgumentException to orElseThrow works.

Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54
  • 1
    I confirm that it does not compile with JDK 1.8.0_51 but Eclipse Mars 4.5.1 compiles it fine... Yet another difference between the two. – Tunaki Jan 05 '16 at 13:48
  • 1
    Yeah, yet another type inference bug. You can simply work-around using `.orElseThrow(() -> new IllegalArgumentException("Empty string"))` – Holger Jan 05 '16 at 16:04

0 Answers0