我正在尝试包装抛出已检查异常的方法。我正在按照此网址中的步骤操作:https ://www.rainerhahnekamp.com/en/ignoring-exceptions-in-java/
有趣的是,当我这样写代码时:
IntStream.range(1, locales.length)
.mapToObj(i -> locales[i].toString())
.forEach(wrap(this::testLocale));
它工作正常但是当我这样写时:
IntStream.range(1, locales.length)
.mapToObj(i -> locales[i].toString())
.forEach(s -> wrap(testLocale(s)));
Intellij 抱怨“未处理的异常:java.lang.Exception”
这里 testLocale 看起来像这样:
void testLocale(String s) throws Exception
包装函数如下所示:
public static <T> Consumer<T> wrap(WrapConsumer<T> wrapper) {
return t -> {
try {
wrapper.accept(t);
} catch(Exception exception) {
throw new RuntimeException(exception);
}
};
}
而 WrapConsumer 是带有 Consumer 签名的函数接口:
@FunctionalInterface
public interface WrapConsumer<T> {
void accept(T t) throws Exception;
}
我正在努力理解为什么 Intellij 根据我编写 lambda 的方式抱怨
斯蒂芬大帝
慕勒3428872
四季花海
相关分类