Guava Throwables类

1 什么是Guava Throwables类

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

2 Guava Throwables类的语法

public final class Throwables
   extends Object

3 Guava Throwables类的方法

方法 描述
static List<Throwable> getCausalChain(Throwable throwable) 获取一个 Throwable 原因链作为列表。
static Throwable getRootCause(Throwable throwable) 返回 throwable 的最内在原因。
static String getStackTraceAsString(Throwable throwable) 返回一个包含 toString() 结果的字符串,后跟 throwable 的完整递归堆栈跟踪。
static RuntimeException propagate(Throwable throwable) 如果它是 RuntimeException 或 Error 的实例,则按原样传播 throwable,否则作为最后的手段,将其包装在 RuntimeException 中然后传播。
static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType) 当且仅当它是声明类型的实例时,完全按原样传播可抛出。
static void propagateIfPossible(Throwable throwable) 当且仅当它是 RuntimeException 或 Error 的实例时,完全按原样传播 throwable。
static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) 当且仅当它是 RuntimeException、Error 或 DeclarationType 的实例时,完全按原样传播 throwable。
static <X1 extends Throwable,X2 extends Throwable>void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) 完全按原样传播 throwable,当且仅当它是 RuntimeException、Error、clarifiedType1 或 DeclarationType2 的实例。

5 Guava Throwables类的例子

让我们看一个简单的Guava Throwables类示例。

package com.yiidian;

import com.google.common.base.Throwables;

import java.io.IOException;

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 {
}

输出结果为:

热门文章

优秀文章