1. 程式人生 > 程式設計 >java 8 lambda表示式中的異常處理操作

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,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異常,希望能給大家一些幫助。

補充知識:java8常用的函式,以及lamda表示式有非執行異常能否在外部捕獲

Stream API中經常使用的函式式介面

函式式介面 引數型別 返回型別 描述
Supplier<T> T 提供一個T型別的值
Consumer<T> T void 處理一個T型別的值
BiConsumer<T,U> T,U void 處理T型別和U型別的值
Predicate<T> T boolean 一個計算Boolean值的函式
ToIntFunction<T> T int 計算int值的函式
ToLongFunction<T> T long 計算long值的函式
ToDoubleFunction<T> T double 計算double的函式
IntFunction<R> int R 引數為int型別的函式(特別注意)
LongFunction<R> long R 引數為long型別的函式
DoubleFunction<R> double R 引數型別為double的函式
Function<T,R> T R 一個引數型別為T的函式
BiFunction<T,U,R> T,U R 一個引數為T和U的函式
UnaryOperator<T> T T 對T進行一元操作
BinaryOperator<T> T,T T 對T進行二元操作

lamda常用的函式式介面

函式式介面 引數型別 返回型別 抽象方法名 描述 其他方法
Runnable void run 執行一個沒有引數和返回值的操作
Supplier<T> T get 提供一個T型別的值
Consumer<T> T void accept 處理一個T型別的值 chain
BiConsumer<T,U void accept 處理T型別和U型別的值 chain
Function<T,R> T R apply 一個引數型別為T的函式 compose,andThen,identity
BiFunction<T,U R apply 一個引數型別為T和U的函式值 andThen
UnaryOperator<T> T T apply 對型別T進行的一元操作 compose,identity
BinaryOperator<T> T,T T apply 對型別T進行的二元操作 andThen
Predicate<T> T boolean test 一個計算boolean值的函式 And,or,negate,isEqual
BiPredicate<T,U boolean test 一個含有兩個引數,計算boolean的函式 and,negate

未宣告丟擲異常的表示式在使用的時候 只能在呼叫表示式的方法外捕獲

假如有個方法的引數是個表示式

我們傳入表示式的時候不能在呼叫這個方法的語句外直接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();
 }
});

這個問題很小但是一定要搞清楚,我們這個表示式宣告沒有丟擲異常

我們執行表示式的方法也沒有處理或者丟擲表示式可能發生的異常,因而我們呼叫test這個方法傳入的表示式要自己處理異常。

Q:但是到底可以不可在test外部能不能接住表示式的異常的?

A:其實外部的try是可以捕獲表示式內的語句的異常的。如果遇到必須在傳入的表示式實現中處理異常,那只是編譯的時候語法檢查不過,要麼是處理表達式的方法test沒有丟擲異常要麼是沒有處理異常

tips: 如何避免在表示式內寫try-catch

①將public static void testThrowExceptions() throws Exception宣告

改為

public static void testThrowExceptions() throws RuntimeException

public static void testThrowExceptions()

這樣避免在表示式內被強制處理非執行時異常,因為你的表示式內容沒有顯示的丟擲非執行時異常。

②處理表達式的test方法宣告丟擲或者內部處理異常,或者表示式宣告本身丟擲異常

我們體驗下,這樣就能在外部捕獲表示式內發生的異常了。

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表示式中的異常處理操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。