簡(jiǎn)介
java 8中引入了lambda表達(dá)式老厌,lambda表達(dá)式可以讓我們的代碼更加簡(jiǎn)介楷拳,業(yè)務(wù)邏輯更加清晰,但是在lambda表達(dá)式中使用的Functional Interface并沒(méi)有很好的處理異常,因?yàn)镴DK提供的這些Functional Interface通常都是沒(méi)有拋出異常的,這意味著需要我們自己手動(dòng)來(lái)處理異常啡浊。
因?yàn)楫惓7譃閁nchecked Exception和checked Exception,我們分別來(lái)討論。
處理Unchecked Exception
Unchecked exception也叫做RuntimeException胶背,出現(xiàn)RuntimeException通常是因?yàn)槲覀兊拇a有問(wèn)題巷嚣。RuntimeException是不需要被捕獲的。也就是說(shuō)如果有RuntimeException钳吟,沒(méi)有捕獲也可以通過(guò)編譯廷粒。
我們看一個(gè)例子:
List<Integer> integers = Arrays.asList(1,2,3,4,5);
integers.forEach(i -> System.out.println(1 / i));
這個(gè)例子是可以編譯成功的,但是上面有一個(gè)問(wèn)題红且,如果list中有一個(gè)0的話(huà)坝茎,就會(huì)拋出ArithmeticException。
雖然這個(gè)是一個(gè)Unchecked Exception直焙,但是我們還是想處理一下:
integers.forEach(i -> {
try {
System.out.println(1 / i);
} catch (ArithmeticException e) {
System.err.println(
"Arithmetic Exception occured : " + e.getMessage());
}
});
上面的例子我們使用了try景东,catch來(lái)處理異常砂轻,簡(jiǎn)單但是破壞了lambda表達(dá)式的最佳實(shí)踐奔誓。代碼變得臃腫。
我們將try搔涝,catch移到一個(gè)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());
}
};
}
則原來(lái)的調(diào)用變成這樣:
integers.forEach(lambdaWrapper(i -> System.out.println(1 / i)));
但是上面的wrapper固定了捕獲ArithmeticException厨喂,我們?cè)賹⑵涓木幊梢粋€(gè)更通用的類(lèi):
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;
}
}
};
}
上面的類(lèi)傳入一個(gè)class,并將其cast到異常庄呈,如果能cast蜕煌,則處理,否則拋出異常诬留。
這樣處理之后斜纪,我們這樣調(diào)用:
integers.forEach(
consumerWrapperWithExceptionClass(
i -> System.out.println(1 / i),
ArithmeticException.class));
處理checked Exception
checked Exception是必須要處理的異常贫母,我們還是看個(gè)例子:
static void throwIOException(Integer integer) throws IOException {
}
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
integers.forEach(i -> throwIOException(i));
上面我們定義了一個(gè)方法拋出IOException,這是一個(gè)checked Exception盒刚,需要被處理腺劣,所以在下面的forEach中,程序會(huì)編譯失敗因块,因?yàn)闆](méi)有處理相應(yīng)的異常橘原。
最簡(jiǎn)單的辦法就是try,catch住涡上,如下所示:
integers.forEach(i -> {
try {
throwIOException(i);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
當(dāng)然趾断,這樣的做法的壞處我們?cè)谏厦嬉呀?jīng)講過(guò)了,同樣的吩愧,我們可以定義一個(gè)新的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異常芋酌,希望能給大家一些幫助。
愿與諸君共進(jìn)步雁佳,大量的面試題及答案還有資深架構(gòu)師錄制的視頻錄像:有Spring隔嫡,MyBatis,Netty源碼分析甘穿,高并發(fā)腮恩、高性能、分布式温兼、微服務(wù)架構(gòu)的原理秸滴,JVM性能優(yōu)化、分布式架構(gòu)等這些成為架構(gòu)師必備的知識(shí)體系募判,可以微信搜索539413949獲取荡含,最后祝大家都能拿到自己心儀的offer