java 8 lambda表達(dá)式中的異常處理操作
簡介
java 8中引入了lambda表達(dá)式,lambda表達(dá)式可以讓我們的代碼更加簡介,業(yè)務(wù)邏輯更加清晰,但是在lambda表達(dá)式中使用的Functional Interface并沒有很好的處理異常,因為JDK提供的這些Functional Interface通常都是沒有拋出異常的,這意味著需要我們自己手動來處理異常。
因為異常分為Unchecked Exception和checked Exception,我們分別來討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException,出現(xiàn)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表達(dá)式的最佳實踐。代碼變得臃腫。
我們將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()); } }; }
則原來的調(diào)用變成這樣:
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,則處理,否則拋出異常。
這樣處理之后,我們這樣調(diào)用:
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中,程序會編譯失敗,因為沒有處理相應(yīng)的異常。
最簡單的辦法就是try,catch住,如下所示:
integers.forEach(i -> { try { throwIOException(i); } catch (IOException e) { throw new RuntimeException(e); } });
當(dāng)然,這樣的做法的壞處我們在上面已經(jīng)講過了,同樣的,我們可以定義一個新的wrapper方法:
static <T> Consumer<T> consumerWrapper( ThrowingConsumer<T, Exception> throwingConsumer) { return i -> { try { throwingConsumer.accept(i); } catch (Exception ex) { throw new RuntimeException(ex); } }; }
我們這樣調(diào)用:
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); } } }; }
然后這樣調(diào)用:
integers.forEach(consumerWrapperWithExceptionClass(
i -> throwIOException(i), IOException.class));
總結(jié)
本文介紹了如何在lambda表達(dá)式中處理checked和unchecked異常,希望能給大家一些幫助。
補(bǔ)充知識:java8常用的函數(shù),以及l(fā)amda表達(dá)式有非運行異常能否在外部捕獲
Stream API中經(jīng)常使用的函數(shù)式接口
函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 描述 |
---|---|---|---|
Supplier<T> | 無 | T | 提供一個T類型的值 |
Consumer<T> | T | void | 處理一個T類型的值 |
BiConsumer<T,U> | T, U | void | 處理T類型和U類型的值 |
Predicate<T> | T | boolean | 一個計算Boolean值的函數(shù) |
ToIntFunction<T> | T | int | 計算int值的函數(shù) |
ToLongFunction<T> | T | long | 計算long值的函數(shù) |
ToDoubleFunction<T> | T | double | 計算double的函數(shù) |
IntFunction<R> | int | R | 參數(shù)為int類型的函數(shù)(特別注意) |
LongFunction<R> | long | R | 參數(shù)為long類型的函數(shù) |
DoubleFunction<R> | double | R | 參數(shù)類型為double的函數(shù) |
Function<T,R> | T | R | 一個參數(shù)類型為T的函數(shù) |
BiFunction<T,U,R> | T,U | R | 一個參數(shù)為T和U的函數(shù) |
UnaryOperator<T> | T | T | 對T進(jìn)行一元操作 |
BinaryOperator<T> | T,T | T | 對T進(jìn)行二元操作 |
lamda常用的函數(shù)式接口
函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 抽象方法名 | 描述 | 其他方法 |
---|---|---|---|---|---|
Runnable | 無 | void | run | 執(zhí)行一個沒有參數(shù)和返回值的操作 | |
Supplier<T> | 無 | T | get | 提供一個T類型的值 | |
Consumer<T> | T | void | accept | 處理一個T類型的值 | chain |
BiConsumer<T,U> | T,U | void | accept | 處理T類型和U類型的值 | chain |
Function<T,R> | T | R | apply | 一個參數(shù)類型為T的函數(shù) | compose,andThen,identity |
BiFunction<T,U,R> | T,U | R | apply | 一個參數(shù)類型為T和U的函數(shù)值 | andThen |
UnaryOperator<T> | T | T | apply | 對類型T進(jìn)行的一元操作 | compose,andThen,identity |
BinaryOperator<T> | T,T | T | apply | 對類型T進(jìn)行的二元操作 | andThen |
Predicate<T> | T | boolean | test | 一個計算boolean值的函數(shù) | And,or,negate,isEqual |
BiPredicate<T,U> | T,U | boolean | test | 一個含有兩個參數(shù),計算boolean的函數(shù) | and,or,negate |
未聲明拋出異常的表達(dá)式在使用的時候 只能在調(diào)用表達(dá)式的方法外捕獲
假如有個方法的參數(shù)是個表達(dá)式
我們傳入表達(dá)式的時候不能在調(diào)用這個方法的語句外直接try捕獲
public static void testThrowExceptions() throws Exception { int[] arr = new int[0]; arr[0] = 10; } public static void test(Runnable call) { call.run(); }
不能寫為
編譯不過
try { test(() ->testThrowExceptions()); } catch (Exception e) { }
而要寫為
test(() -> { try { testThrowExceptions(); } catch (Exception e) { e.printStackTrace(); } });
這個問題很小但是一定要搞清楚,我們這個表達(dá)式聲明沒有拋出異常
我們執(zhí)行表達(dá)式的方法也沒有處理或者拋出表達(dá)式可能發(fā)生的異常,因而我們調(diào)用test這個方法傳入的表達(dá)式要自己處理異常。
Q:但是到底可以不可在test外部能不能接住表達(dá)式的異常的?
A:其實外部的try是可以捕獲表達(dá)式內(nèi)的語句的異常的。如果遇到必須在傳入的表達(dá)式實現(xiàn)中處理異常,那只是編譯的時候語法檢查不過,要么是處理表達(dá)式的方法test沒有拋出異常要么是沒有處理異常
tips: 如何避免在表達(dá)式內(nèi)寫try-catch
①將public static void testThrowExceptions() throws Exception聲明
改為
public static void testThrowExceptions() throws RuntimeException
或
public static void testThrowExceptions()
這樣避免在表達(dá)式內(nèi)被強(qiáng)制處理非運行時異常,因為你的表達(dá)式內(nèi)容沒有顯示的拋出非運行時異常。
②處理表達(dá)式的test方法聲明拋出或者內(nèi)部處理異常,或者表達(dá)式聲明本身拋出異常
我們體驗下,這樣就能在外部捕獲表達(dá)式內(nèi)發(fā)生的異常了。
public static void test(Callable<String> call) throws Exception { call.call(); } public static void main(String args[]) { try { test(() -> { testThrowExceptions(); return "ok"; }); } catch (Exception e) { System.out.println("catche"+e); e.printStackTrace(); } }
或者
public static void testThrowExceptions() throws Exception { int[] arr = new int[0]; arr[0] = 10; } public static void test(Callable<String> call) { try { call.call(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]) { test(() -> { testThrowExceptions(); return "ok"; }); }
以上這篇java 8 lambda表達(dá)式中的異常處理操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解讀@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstr
這篇文章主要介紹了解讀@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstructor的區(qū)別及在springboot常用地方,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot核心@SpringBootApplication使用介紹
這篇文章主要介紹了SpringBoot核心@SpringBootApplication的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java創(chuàng)建非阻塞的HTTP服務(wù)器的實現(xiàn)
本文主要介紹了Java創(chuàng)建非阻塞的HTTP服務(wù)器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04Springboot注入成員變量HttpServletRequest的原理分析
這篇文章主要介紹了Springboot注入成員變量HttpServletRequest的原理分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Spring @Value 設(shè)置默認(rèn)值的實現(xiàn)
這篇文章主要介紹了Spring @Value 設(shè)置默認(rèn)值的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09