java 8 lambda表達式中的異常處理
簡介
java 8中引入了lambda表達式,lambda表達式可以讓我們的代碼更加簡介,業務邏輯更加清晰,但是在lambda表達式中使用的Functional Interface並沒有很好的處理異常,因為JDK提供的這些Functional Interface通常都是沒有拋出異常的,這意味着需要我們自己手動來處理異常。
因為異常分為Unchecked Exception和checked Exception,我們分別來討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException,出現RuntimeException通常是因為我們的代碼有問題。RuntimeException是不需要被捕獲的。也就是說如果有RuntimeException,沒有捕獲也可以通過編譯。
我們看一個例子:
List<Integer> integers = Arrays.asList(1,2,3,4,5);
integers.forEach(i -> System.out.println(1 / i));
這個例子是可以編譯成功的,但是上面有一個問題,如果list中有一個0的話,就會拋出ArithmeticException。
雖然這個是一個Unchecked Exception,但是我們還是想處理一下:
integers.forEach(i -> {
try {
System.out.println(1 / i);
} catch (ArithmeticException e) {
System.err.println(
"Arithmetic Exception occured : " + e.getMessage());
}
});
上面的例子我們使用了try,catch來處理異常,簡單但是破壞了lambda表達式的最佳實踐。代碼變得臃腫。
我們將try,catch移到一個wrapper方法中:
static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) {
return i -> {
try {
consumer.accept(i);
} catch (ArithmeticException e) {
System.err.println(
"Arithmetic Exception occured : " + e.getMessage());
}
};
}
則原來的調用變成這樣:
integers.forEach(lambdaWrapper(i -> System.out.println(1 / i)));
但是上面的wrapper固定了捕獲ArithmeticException,我們再將其改編成一個更通用的類:
static <T, E extends Exception> Consumer<T>
consumerWrapperWithExceptionClass(Consumer<T> consumer, Class<E> 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;
}
}
};
}
上面的類傳入一個class,並將其cast到異常,如果能cast,則處理,否則拋出異常。
這樣處理之后,我們這樣調用:
integers.forEach(
consumerWrapperWithExceptionClass(
i -> System.out.println(1 / i),
ArithmeticException.class));
處理checked Exception
checked Exception是必須要處理的異常,我們還是看個例子:
static void throwIOException(Integer integer) throws IOException {
}
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
integers.forEach(i -> throwIOException(i));
上面我們定義了一個方法拋出IOException,這是一個checked Exception,需要被處理,所以在下面的forEach中,程序會編譯失敗,因為沒有處理相應的異常。
最簡單的辦法就是try,catch住,如下所示:
integers.forEach(i -> {
try {
throwIOException(i);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
當然,這樣的做法的壞處我們在上面已經講過了,同樣的,我們可以定義一個新的wrapper方法:
static <T> Consumer<T> consumerWrapper(
ThrowingConsumer<T, Exception> throwingConsumer) {
return i -> {
try {
throwingConsumer.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
我們這樣調用:
integers.forEach(consumerWrapper(i -> throwIOException(i)));
我們也可以封裝一下異常:
static <T, E extends Exception> Consumer<T> consumerWrapperWithExceptionClass(
ThrowingConsumer<T, E> throwingConsumer, Class<E> 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);
}
}
};
}
然后這樣調用:
integers.forEach(consumerWrapperWithExceptionClass(
i -> throwIOException(i), IOException.class));
總結
本文介紹了如何在lambda表達式中處理checked和unchecked異常,希望能給大家一些幫助。
本文的例子https://github.com/ddean2009/learn-java-streams/tree/master/lambda-exception
更多內容請訪問 www.flydean.com
最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!
歡迎關注我的公眾號:「程序那些事」,懂技術,更懂你!