Guava Throwables类

来源:这里教程网 时间:2026-02-17 21:39:20 作者:

Guava Throwables类

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

类声明

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

public final class Throwables   extends Object

类方法

序号方法和描述
1static List <Throwable> getCausalChain(Throwable throwable) 获取Throwable的因果链作为列表。
2static Throwable getRootCause(Throwable throwable) 返回throwable的最内层原因。
3static String getStackTraceAsString(Throwable throwable) 返回一个包含toString()方法的结果,后跟throwable的完整递归堆栈跟踪的字符串。
4static RuntimeException propagate(Throwable throwable) 如果throwable是RuntimeException或Error的实例,则按原样传播;否则,作为最后的手段,在RuntimeException中包装它,然后传播。
5static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType) 如果throwable是declaredType的实例,则按原样传播。
6static void propagateIfPossible(Throwable throwable) 如果throwable是RuntimeException或Error的实例,则按原样传播。
7static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) 如果throwable是RuntimeException、Error或declaredType的实例,则按原样传播。
8static <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类的示例

使用任意编辑器在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

查看结果。

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

相关推荐