Guava - Throwables 類



Throwables 類提供與 Throwable 介面相關的實用程式方法。

類宣告

以下是com.google.common.base.Throwables類的宣告:

public final class Throwables
   extends Object

類方法

序號 方法和描述
1

static List<Throwable> getCausalChain(Throwable throwable)

將 Throwable 異常鏈作為列表獲取。

2

static Throwable getRootCause(Throwable throwable)

返回throwable的最根本原因。

3

static String getStackTraceAsString(Throwable throwable)

返回一個字串,其中包含toString()的結果,以及throwable的完整遞迴堆疊跟蹤。

4

static RuntimeException propagate(Throwable throwable)

如果throwable是RuntimeException或Error的例項,則按原樣傳播;否則,作為最後手段,將其包裝在RuntimeException中,然後傳播。

5

static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType)

僅當throwable是declaredType的例項時,才按原樣傳播。

6

static void propagateIfPossible(Throwable throwable)

僅當throwable是RuntimeException或Error的例項時,才按原樣傳播。

7

static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType)

僅當throwable是RuntimeException、Error或declaredType的例項時,才按原樣傳播。

8

static <X1 extends Throwable,X2 extends Throwable>void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2)

僅當throwable是RuntimeException、Error、declaredType1或declaredType2的例項時,才按原樣傳播。

繼承的方法

此類繼承自以下類的方法:

  • java.lang.Object

Throwables類的示例

使用您選擇的任何編輯器建立以下Java程式,例如在C:/> Guava目錄下。

GuavaTester.java

import java.io.IOException;

import com.google.common.base.Objects;
import com.google.common.base.Throwables;

public class GuavaTester {
   public static void main(String args[]) {
   
      GuavaTester tester = new GuavaTester();

      try {
         tester.showcaseThrowables();
         
      } catch (InvalidInputException e) {
         //get the root cause
         System.out.println(Throwables.getRootCause(e));
      
      } catch (Exception e) {
         //get the stack trace in string format
         System.out.println(Throwables.getStackTraceAsString(e));
      }

      try {
         tester.showcaseThrowables1();

      } catch (Exception e) {
         System.out.println(Throwables.getStackTraceAsString(e));
      }
   }

   public void showcaseThrowables() throws InvalidInputException {
      try {
         sqrt(-3.0);
      } catch (Throwable e) {
         //check the type of exception and throw it
         Throwables.propagateIfInstanceOf(e, InvalidInputException.class);
         Throwables.propagate(e);
      }
   }

   public void showcaseThrowables1() {
      try {
         int[] data = {1,2,3};
         getValue(data, 4);
      } catch (Throwable e) {
         Throwables.propagateIfInstanceOf(e, IndexOutOfBoundsException.class);
         Throwables.propagate(e);
      }
   }

   public double sqrt(double input) throws InvalidInputException {
      if(input < 0) throw new InvalidInputException();
      return Math.sqrt(input);
   }

   public double getValue(int[] list, int index) throws IndexOutOfBoundsException {
      return list[index];
   }

   public void dummyIO() throws IOException {
      throw new IOException();
   }
}

class InvalidInputException extends Exception {
}

驗證結果

使用javac編譯器編譯類,如下所示:

C:\Guava>javac GuavaTester.java

現在執行GuavaTester檢視結果。

C:\Guava>java GuavaTester

檢視結果。

InvalidInputException
java.lang.ArrayIndexOutOfBoundsException: 4
   at GuavaTester.getValue(GuavaTester.java:52)
   at GuavaTester.showcaseThrowables1(GuavaTester.java:38)
   at GuavaTester.main(GuavaTester.java:19)
廣告