为什么以下代码
CompletionStage<Optional<String>> future = completedFuture(Optional.empty());
future.thenApply(optStr -> {
return optStr.map(s -> s.length())
.orElseThrow(() -> new IllegalArgumentException("Empty string"));
});
无法编译,
unreported exception X; must be caught or declared to be thrown
将IllegalArgumentException
的显式类型参数传递给orElseThrow
。