Throwables类提供与Throwable接口相关的实用程序方法.
以下是 com.google.common的声明.base.Throwables class :
public final class Throwables extends Object
Sr.No | 方法&说明 |
---|---|
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) 如果它是RuntimeException或Error的实例,则按原样传播throwable,否则作为最后的手段,将它包装在RuntimeException中然后传播. |
5 | static< X extends Throwable> void propagateIfInstanceOf(Throwable throwable,Class< X> declaredType) 当且仅当它是declaredType的实例时,完全按原样传播throwable. |
6 | static void propagateIfPossible(Throwable throwable) 当且仅当它是RuntimeException或Error的实例时,才能完全按原样传播throwable. |
7 | static< X extends Throwable> void propagateIfPossible(Throwable throwable,Class< X> declaredType) 当且仅当它是RuntimeException,Error或declaredType的实例时,才能完全按原样传播throwable. |
8 | static< X1 extends Throwable,X2 extends Throwable> void propagateIfPossible(Throwable throwable,Class< X1> declaredType1,Class< X2> declaredType2) 准确传播throwable as-is,当且仅当它是RuntimeException,Error,declaredType1或declaredType2的实例. |
此类继承以下类中的方法 :
java .lang.Object
使用您选择的任何编辑器创建以下java程序说 C:/>番石榴.
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)