我知道如何创建对具有 String
参数并返回 int
的方法的引用,它是:
Function<String, Integer>
但是,如果函数抛出异常,这将不起作用,例如定义为:
Integer myMethod(String s) throws IOException
我将如何定义这个参考?
原文由 Rocky Pulley 发布,翻译遵循 CC BY-SA 4.0 许可协议
我知道如何创建对具有 String
参数并返回 int
的方法的引用,它是:
Function<String, Integer>
但是,如果函数抛出异常,这将不起作用,例如定义为:
Integer myMethod(String s) throws IOException
我将如何定义这个参考?
原文由 Rocky Pulley 发布,翻译遵循 CC BY-SA 4.0 许可协议
您实际上可以使用处理异常的新接口扩展 Consumer
(和 Function
等)——使用Java 8的 默认方法!
考虑这个接口(扩展 Consumer
):
@FunctionalInterface
public interface ThrowingConsumer<T> extends Consumer<T> {
@Override
default void accept(final T elem) {
try {
acceptThrows(elem);
} catch (final Exception e) {
// Implement your own exception handling logic here..
// For example:
System.out.println("handling an exception...");
// Or ...
throw new RuntimeException(e);
}
}
void acceptThrows(T elem) throws Exception;
}
然后,例如,如果您有一个列表:
final List<String> list = Arrays.asList("A", "B", "C");
如果你想使用它(例如 forEach
)和一些抛出异常的代码,你通常会设置一个 try/catch 块:
final Consumer<String> consumer = aps -> {
try {
// maybe some other code here...
throw new Exception("asdas");
} catch (final Exception ex) {
System.out.println("handling an exception...");
}
};
list.forEach(consumer);
但是有了这个新接口,你可以用 lambda 表达式实例化它,编译器就不会报错了:
final ThrowingConsumer<String> throwingConsumer = aps -> {
// maybe some other code here...
throw new Exception("asdas");
};
list.forEach(throwingConsumer);
或者甚至只是让它更简洁!:
list.forEach((ThrowingConsumer<String>) aps -> {
// maybe some other code here...
throw new Exception("asda");
});
看起来 Durian 中有一个非常好的实用程序库部分称为 Errors ,它可以更灵活地用于解决这个问题。例如,在我上面的实现中,我明确定义了错误处理策略( System.out...
或 throw RuntimeException
),而 Durian 的错误允许您通过大型套件即时应用策略实用方法。感谢 分享,@NedTwigg!
示例用法:
list.forEach(Errors.rethrow().wrap(c -> somethingThatThrows(c)));
原文由 jlb 发布,翻译遵循 CC BY-SA 4.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答6k 阅读
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
您需要执行以下操作之一。
并使用它:
Integer myMethod(String s)
包装在不声明检查异常的方法中:接着:
或者: