Guava 简明教程

Guava - Throwables Class

Throwable 类提供了与 Throwable 接口相关的实用方法。

Class Declaration

以下是 com.google.common.base.Throwables 类的声明 -

public final class Throwables
   extends Object

Class Methods

Sr.No

Method & Description

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 的实例时,才按原样传播 Throwable。

6

static void propagateIfPossible(Throwable throwable) 当且仅当 Throwable 是 RuntimeException 或 Error 的实例时,才按原样传播 Throwable。

7

static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) 当且仅当 Throwable 是 RuntimeException、Error 或 declaredType 的实例时,才按原样传播 Throwable。

8

static <X1 extends Throwable,X2 extends Throwable>void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) 当且仅当 Throwable 是 RuntimeException、Error、declaredType1 或 declaredType2 的实例时,才按原样传播 Throwable。

Methods Inherited

此类从以下类继承方法 −

  1. java.lang.Object

Example of Throwables Class

使用任意你选择的编辑器在 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 {
}

Verify the Result

按照如下方式使用 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)