📜  Guava-Throwables类

📅  最后修改于: 2020-11-16 06:53:12             🧑  作者: Mango


Throwables类提供与Throwable接口相关的实用程序方法。

类声明

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

public final class Throwables
   extends Object

类方法

Sr.No Method & Description
1

static List getCausalChain(Throwable throwable)

Gets a Throwable cause chain as a list.

2

static Throwable getRootCause(Throwable throwable)

Returns the innermost cause of throwable.

3

static String getStackTraceAsString(Throwable throwable)

Returns a string containing the result of toString(), followed by the full, recursive stack trace of throwable.

4

static RuntimeException propagate(Throwable throwable)

Propagates throwable as-is if it is an instance of RuntimeException or Error, or else as a last resort, wraps it in a RuntimeException then propagates.

5

static void propagateIfInstanceOf(Throwable throwable, Class declaredType)

Propagates throwable exactly as-is, if and only if it is an instance of declaredType.

6

static void propagateIfPossible(Throwable throwable)

Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException or Error.

7

static void propagateIfPossible(Throwable throwable, Class declaredType)

Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException, Error, or declaredType.

8

static void propagateIfPossible(Throwable throwable, Class declaredType1, Class declaredType2)

Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException, Error, declaredType1, or declaredType2.

继承的方法

此类从以下类继承方法-

  • java.lang.Object

可投掷类示例

使用您选择的任何编辑器在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)