为来自 lambda 表达式的异常生成功能接口,并将它们扔到方法的签名上
Generify Funtional Interfaces for exceptions from a lambda expression and throw them on the signature of the method
仅供版主:这不是一个重复的问题。
我需要一些非常困难的事情的答案:从 lambda 表达式中抛出异常。
我位于 link: https://www.baeldung.com/java-lambda-exceptions(还有很多其他 link,我不必在这里写),我编写了这段代码哪个有效。
不得不说,只有看过link的人才能看懂我在写什么
---- myMethod 抛出异常-----
boolean myMethod(element, field, filter) {
// code which throws these: NoSuchMethodException, InvocationTargetException, IllegalAccessException
}
----我代码中某处感兴趣的母亲方法-----
public <T> List<T> mamaMethod(List<T> list, String field, String filter) {
return list == null ? null :
list.stream().filter(myMehtod(element, field, filter)).collect(Collectors.toList());
}
// Here the "myMethod" throws these: NoSuchMethodException, InvocationTargetException, IllegalAccessException
根据link
我的解决方案
-----我的功能界面-----
@FunctionalInterface
public interface PredicateWith3Exceptions<T, E1 extends Exception, E2 extends Exception, E3 extends Exception> {
boolean test(T element) throws E1, E2, E3;
}
----我的Wrapper方法----
private <T, E1 extends NoSuchMethodException, E2 extends InvocationTargetException, E3 extends IllegalAccessException>
Predicate<T> wrapper(PredicateWith3Exceptions<T, E1, E2, E3> predicate) {
return element -> {
try {
return predicate.test(element);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
};
}
--- 放在mother方法中---
public <T> List<T> list mamaMethod(List<T> list, String field, String filter) {
return list == null ? null :
list.stream().filter(wrapper(
(PredicateWith3Exceptions<T, NoSuchMethodException, InvocationTargetException, IllegalAccessException>)
element -> myMehtod(element, field, filter))).collect(Collectors.toList());
// In the link there is not casting, but my intelliJ accepts it only with casting to PredicateWith3Exceptions
}
以上代码运行良好。但是,我的问题:
如何生成 PredicateWith3Exceptions,以处理任意数量的异常。
异常在包装器中被捕获。我希望它们不被捕获,而是在母方法上使用 throws 语句重新抛出,例如:
public <T> List<T> mamaMethod(...) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
- 关于 RuntimeException...我不明白为什么我读到的所有 links 都将 RuntimeException 作为最终解决方案。别人是这样做的
特定于 CheckedIOException。所有这些都是未经检查的异常,我不明白为什么我必须将异常静音,这将是
无论如何都会抛出它自己,它会杀死我的应用程序。
如果以上没看懂,我可以重写问题
非常感谢
谷歌搜索后我发现:
问题 1:在 java 中无法创建可变数量的泛型类型。
问题2:如果我不想在lambda表达式内部尝试catch,我可以在我撤销这个母方法的地方捕获RuntimeException异常
问题 3:我不应该抛出如此通用的异常。我可以扔一个 UncheckedIOException,或者我可以创建的另一个未经检查的异常来扩展 RuntimeException。
虽然我个人不喜欢未经检查的异常,但在这种情况下是强制性的,因为需要在已经抛出的地方捕获已检查的异常。它们不能通过母方法签名中的抛出语句被抛出到更高级别。
但是在这种情况下,只要mother方法的最终使用者没有这个RuntimeException的线索,他就不可能知道自己要捕捉它。
作为一些链接,我发现这种做法是糟糕的编程实践和代码味道。
任何可以对这些想法发表意见的人都可以随意添加为答案。
作为解决方案,我将关闭此作为我的答案。
仅供版主:这不是一个重复的问题。
我需要一些非常困难的事情的答案:从 lambda 表达式中抛出异常。
我位于 link: https://www.baeldung.com/java-lambda-exceptions(还有很多其他 link,我不必在这里写),我编写了这段代码哪个有效。 不得不说,只有看过link的人才能看懂我在写什么
---- myMethod 抛出异常-----
boolean myMethod(element, field, filter) {
// code which throws these: NoSuchMethodException, InvocationTargetException, IllegalAccessException
}
----我代码中某处感兴趣的母亲方法-----
public <T> List<T> mamaMethod(List<T> list, String field, String filter) {
return list == null ? null :
list.stream().filter(myMehtod(element, field, filter)).collect(Collectors.toList());
}
// Here the "myMethod" throws these: NoSuchMethodException, InvocationTargetException, IllegalAccessException
根据link
我的解决方案-----我的功能界面-----
@FunctionalInterface
public interface PredicateWith3Exceptions<T, E1 extends Exception, E2 extends Exception, E3 extends Exception> {
boolean test(T element) throws E1, E2, E3;
}
----我的Wrapper方法----
private <T, E1 extends NoSuchMethodException, E2 extends InvocationTargetException, E3 extends IllegalAccessException>
Predicate<T> wrapper(PredicateWith3Exceptions<T, E1, E2, E3> predicate) {
return element -> {
try {
return predicate.test(element);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
};
}
--- 放在mother方法中---
public <T> List<T> list mamaMethod(List<T> list, String field, String filter) {
return list == null ? null :
list.stream().filter(wrapper(
(PredicateWith3Exceptions<T, NoSuchMethodException, InvocationTargetException, IllegalAccessException>)
element -> myMehtod(element, field, filter))).collect(Collectors.toList());
// In the link there is not casting, but my intelliJ accepts it only with casting to PredicateWith3Exceptions
}
以上代码运行良好。但是,我的问题:
如何生成 PredicateWith3Exceptions,以处理任意数量的异常。
异常在包装器中被捕获。我希望它们不被捕获,而是在母方法上使用 throws 语句重新抛出,例如:
public <T> List<T> mamaMethod(...) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
- 关于 RuntimeException...我不明白为什么我读到的所有 links 都将 RuntimeException 作为最终解决方案。别人是这样做的 特定于 CheckedIOException。所有这些都是未经检查的异常,我不明白为什么我必须将异常静音,这将是 无论如何都会抛出它自己,它会杀死我的应用程序。
如果以上没看懂,我可以重写问题
非常感谢
谷歌搜索后我发现:
问题 1:在 java 中无法创建可变数量的泛型类型。
问题2:如果我不想在lambda表达式内部尝试catch,我可以在我撤销这个母方法的地方捕获RuntimeException异常
问题 3:我不应该抛出如此通用的异常。我可以扔一个 UncheckedIOException,或者我可以创建的另一个未经检查的异常来扩展 RuntimeException。 虽然我个人不喜欢未经检查的异常,但在这种情况下是强制性的,因为需要在已经抛出的地方捕获已检查的异常。它们不能通过母方法签名中的抛出语句被抛出到更高级别。 但是在这种情况下,只要mother方法的最终使用者没有这个RuntimeException的线索,他就不可能知道自己要捕捉它。 作为一些链接,我发现这种做法是糟糕的编程实践和代码味道。
任何可以对这些想法发表意见的人都可以随意添加为答案。 作为解决方案,我将关闭此作为我的答案。