崂山网站建设,免费解析网站,江阴网站建设公司,浅谈高校门户网站建设的规范标准lambda表达式无法抛出异常
背景#xff1a;在一个方法中使用了lambda表达式#xff0c;表达式中需要捕获异常#xff0c;使用throws关键字发现并不起作用#xff0c;必须使用trycatch才行
public class BeanUtil {public static T,R ListR copyList(List…lambda表达式无法抛出异常
背景在一个方法中使用了lambda表达式表达式中需要捕获异常使用throws关键字发现并不起作用必须使用trycatch才行
public class BeanUtil {public static T,R ListR copyList(ListT source , ClassR clazz) throws Exception {if(CollectionUtils.isEmpty(source)){return null;}ListR rList source.stream().map(s -{R r null;r clazz.newInstance();BeanUtils.copyProperties(s,r);return r;} ).collect(Collectors.toList());return rList;}
}如上虽然在方法copyList上使用了throws Exception但是已经无法通过编译提示有未处理异常正确代码如下使用trycatch try {r clazz.newInstance();} catch (Exception e) {e.printStackTrace();}提问为何lambda表达式中的异常不能在外部throws只能内部捕获
看Stream.map(Function? super T, ? extends R mapper)方法参数为Function实例源码
public final R StreamR map(Function? super P_OUT, ? extends R mapper) {Objects.requireNonNull(mapper);return new StatelessOpP_OUT, R(this, StreamShape.REFERENCE,StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {OverrideSinkP_OUT opWrapSink(int flags, SinkR sink) {return new Sink.ChainedReferenceP_OUT, R(sink) {Overridepublic void accept(P_OUT u) {downstream.accept(mapper.apply(u));}};}};}最终调用了Function中的apply方法事实上我们在map方法中写的s -{……},其实就是重写apply方法的实现而抛出异常的语句r clazz.newInstance()就是在apply方法中抛出的就是说在异常语句和copyList方法之间还存在一个apply方法而apply方法是不允许向上抛出异常的所以copyList方法自然不能使用throws抛异常。 Lambda异常处理
java8 lambda表达式利用函数式编程提供精简的方式表达行为。然而JDK函数式接口没有很好地处理异常使得处理异常代码非常臃肿和麻烦。本文探讨在lambda表达式中处理异常的一些方式。
处理非检查异常
首先我们通过示例来说明问题。有List和常量除比如50和list中每个元素除并打印出结果
ListInteger integers Arrays.asList(3, 9, 7, 6, 10, 20);
integers.forEach(i - System.out.println(50 / i));上述代码正常工作但有问题。如果list有元素值为0那么会抛出异常ArithmeticException: / by zero。我们利用传统的try-catch块处理该异常打印异常内容并继续执行一个元素
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(i - {try {System.out.println(50 / i);} catch (ArithmeticException e) {System.err.println(Arithmetic Exception occured : e.getMessage());}
});利用try-catch块解决了问题但没有了lambda表达式的精简性不再是一个小函数了。为了解决这个问题我们给lambda函数写一个lambda包装器。请看代码
static ConsumerInteger lambdaWrapper(ConsumerInteger consumer) {return i - {try {consumer.accept(i);} catch (ArithmeticException e) {System.err.println(Arithmetic Exception occured : e.getMessage());}};
}然后进行调用
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(lambdaWrapper(i - System.out.println(50 / i)));上面代码我们首先定义包装方法负责处理异常然后将其作为参数传给遍历方法。通过包装方法解决了问题但你可能争辩这仅仅从一个地方删除try-catch移动到另一个方法中实际并没有减少代码。
这时事实因为包装器仅给特定的类型使用但我们可以使用泛型提升包装方法的使用范围
static T, E extends Exception ConsumerTconsumerWrapper(ConsumerT consumer, ClassE clazz) {return i - {try {consumer.accept(i);} catch (Exception ex) {try {E exCast clazz.cast(ex);System.err.println(Exception occured : exCast.getMessage());} catch (ClassCastException ccEx) {throw ex;}}};
}调用代码
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(consumerWrapper(i - System.out.println(50 / i), ArithmeticException.class));12你看到包装方法带两个参数lamdba表达式和捕获异常类型。lamdba包装器能够处理任何类型不仅是Integer也可以捕获任何类型的异常而不仅是超类Exception。
你可能注意到包装器的方法名从lambdaWrapper改成了consumerWrapper。那是因为这个方法仅处理Consumer类型的函数接口的lambda表达式。同样我们也可以定义其他函数接口的包装方法,如Function, BiFunction, BiConsumer 等。
处理检查异常
考虑之前的示例我们不再除元素并打印结果至控制台我们想写至文件该操作会抛出异常IOException:
static void writeToFile(Integer integer) throws IOException {// logic to write to file which throws IOException
}
java
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(i - writeToFile(i));一旦编译会出现下面错误
java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException因为IOException异常是检查异常代码必须处理。现在我们有两种方法通过throw签名方法让调用者处理或在lambda方法内部处理。下面分别进行讨论。
从Lambda表达式中throw检查异常
在包括lambda表达式的方法上通过throw抛出异常
public static void main(String[] args) throws IOException {ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);integers.forEach(i - writeToFile(i));
}
然而编译仍然报相同的未处理异常IOException因为lambda表达式类似于匿名内部类。在这种情况下lambda表达式是带有accept(T t)方法的Consumer接口的实现。从main方法中throw异常没有意义因为在父接口中的方法没有throw任何异常其实现中也不能有throw异常
ConsumerInteger consumer new ConsumerInteger() {Overridepublic void accept(Integer integer) throws Exception {writeToFile(integer);}
};
上面代码不能编译因为accept实现方法不能throw任何异常。最直接方法使用try-catch块并包装检查异常至非检查异常并重新throw
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(i - {try {writeToFile(i);} catch (IOException e) {throw new RuntimeException(e);}
});
通过这个方法获得编译器通过并正常工作但出现了和前面提及的问题一样。既然我们仅项抛出异常我们只需要定义自己的Consumer 函数接口可以抛出异常然后在包装方法中使用下面定义ThrowingConsumer
FunctionalInterface
public interface ThrowingConsumerT, E extends Exception {void accept(T t) throws E;
}1234
static T ConsumerT throwingConsumerWrapper(ThrowingConsumerT, Exception throwingConsumer) {return i - {try {throwingConsumer.accept(i);} catch (Exception ex) {throw new RuntimeException(ex);}};
}
现在我们可以写lambda表达式既可以抛异常也不失简洁性。
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(throwingConsumerWrapper(i - writeToFile(i)));
处理Lambda表达式中检查异常
最后我们仅需要修改包装器使其可以处理检查异常。既然ThrowingConsumer 使用泛型我们能处理任何类型异常
static T, E extends Exception ConsumerT handlingConsumerWrapper(ThrowingConsumerT, E throwingConsumer, ClassE exceptionClass) {return i - {try {throwingConsumer.accept(i);} catch (Exception ex) {try {E exCast exceptionClass.cast(ex);System.err.println(Exception occured : exCast.getMessage());} catch (ClassCastException ccEx) {throw new RuntimeException(ex);}}};
}我们能使用该包装器处理IOException包装器把任何其他检查异常至一个非检查异常
ListInteger integers Arrays.asList(3, 9, 7, 0, 10, 20);
integers.forEach(handlingConsumerWrapper( i - writeToFile(i), IOException.class));与处理非检查异常一样其他类似的函数接口也可类似处理如ThowingFunction, ThrowingBiFunction, ThrowingBiConsumer 。
总结
本文我们介绍了如何使用包装器在lambda表达式中处理特定异常并不失简洁性。学习了如何定义自己的函数式接口通过包装可以把检查异常变为非检查异常并处理。