Java語法中Lambda表達式無法拋出異常的解決
Lambda表達式無法拋出異常
1.Demo 例子
錯誤提示 - Unhandled exception: java.io.IOException;
public static void main(String[] args) throws IOException{
Stream.of("a", "b", "c").forEach(str -> {
throw new IOException();
});
}
2.編譯通過
Stream.of("a", "b", "c").forEach(str -> {
throw new RuntimeException(new IOException());
}); // 不建議使用
或者
static <E extends Exception> void doThrow(Exception e) throws E {
throw (E)e;
}
//編譯通過
Stream.of("a", "b", "c").forEach(str -> {
doThrow(new IOException());
});
lambda表達式異常應(yīng)該如何處理
java 8中引入了lambda表達式,lambda表達式可以讓我們的代碼更加簡介,業(yè)務(wù)邏輯更加清晰,但是在lambda表達式中使用的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表達式的最佳實踐。代碼變得臃腫。
我們將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)用:
ntegers.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住,如下所示:
ntegers.forEach(i -> {
try {
throwIOException(i);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
當然,這樣的做法的壞處我們在上面已經(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));
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?AOP?后置通知修改響應(yīng)httpstatus方式
這篇文章主要介紹了Spring?AOP?后置通知修改響應(yīng)httpstatus方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java設(shè)計模式之中介模式(Mediator模式)介紹
這篇文章主要介紹了Java設(shè)計模式之中介模式(Mediator模式)介紹,本文講解了為何使用Mediator模式、如何使用中介模式等內(nèi)容,需要的朋友可以參考下2015-03-03
java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點
這篇文章主要介紹了java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點,需要的朋友可以參考下2014-02-02

