Guava-throwables-class

提供:Dev Guides
移動先:案内検索

グアバ-投げ物クラス

Throwablesクラスは、Throwableインターフェイスに関連するユーティリティメソッドを提供します。

クラス宣言

以下は com.google.common.base.Throwables クラスの宣言です-

public final class Throwables
   extends Object

クラスメソッド

Sr.No Method & Description
1

static List<Throwable> getCausalChain(Throwable throwable)

スロー可能な原因チェーンをリストとして取得します。

2

static Throwable getRootCause(Throwable throwable)

throwableの最も内側の原因を返します。

3

static String getStackTraceAsString(Throwable throwable)

toString()の結果を含む文字列を返し、その後にthrowableの完全な再帰スタックトレースが続きます。

4

static RuntimeException propagate(Throwable throwable)

RuntimeExceptionまたはErrorのインスタンスである場合、または最後の手段として、スロー可能オブジェクトをそのまま伝播し、RuntimeExceptionにラップしてから伝播します。

5

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

宣言されたタイプのインスタンスである場合にのみ、スロー可能オブジェクトをそのまま伝播します。

6

static void propagateIfPossible(Throwable throwable)

RuntimeExceptionまたはErrorのインスタンスである場合にのみ、スロー可能オブジェクトをそのまま伝播します。

7

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

RuntimeException、Error、またはdeclareTypeのインスタンスである場合にのみ、スロー可能オブジェクトをそのまま伝播します。

8

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

例外がRuntimeException、Error、declaredType1、またはdeclareType2のインスタンスである場合に限り、スロー可能オブジェクトをそのまま伝播します。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • java.lang.Object

Throwablesクラスの例

たとえば、* C:/> Guava。*で選択したエディターを使用して、次のJavaプログラムを作成します。

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)