Java 8 方法参考:提供能够提供参数化结果的供应商

2022-01-22 00:00:00 java-8 java java-stream

我想使用

java.util.Optional.orElseThrow()

具有要求构造函数参数的异常类型.像这样的:

with an Exception type that asks for a constructor parameter. Something like this:

.orElseThrow(MyException::new(someArgument)) // obviously NOT working

有没有办法创建一个供应商来传递我的参数值?

Is there a way to create a Supplier that passes my argument value in?

推荐答案

当然.

.orElseThrow(() -> new MyException(someArgument))

相关文章