- Guava Tutorial
- Guava - Home
- Guava - Overview
- Guava - Environment Setup
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - Collections Utilities
- Guava - Caching Utilities
- Guava - String Utilities
- Guava - Primitive Utilities
- Guava - Math Utilities
- Guava Useful Resources
- Guava - Quick Guide
- Guava - Useful Resources
- Guava - Discussion
Guava - Throwables 类
Throwables 类提供与 Throwable 接口相关的实用方法。
类别声明
以下是com.google.common.base.Throwables类的声明-
public final class Throwables extends Object
类方法
先生编号 | 方法及说明 |
---|---|
1 | static List<Throwable> getCausalChain(Throwable throwable) 获取 Throwable 原因链作为列表。 |
2 | 静态 Throwable getRootCause(Throwable throwable) 返回 throwable 的最内部原因。 |
3 | 静态字符串 getStackTraceAsString(Throwable throwable) 返回一个包含 toString() 结果的字符串,后跟 throwable 的完整递归堆栈跟踪。 |
4 | 静态 RuntimeException 传播(Throwable 抛出) 如果 throwable 是 RuntimeException 或 Error 的实例,则按原样传播,否则作为最后的手段,将其包装在 RuntimeException 中然后传播。 |
5 | static <X extends Throwable> void spreadIfInstanceOf(Throwable throwable, Class<X>clarifiedType) 当且仅当它是声明类型的实例时,才按原样传播 throwable。 |
6 | 静态无效传播IfPossible(Throwable throwable) 当且仅当它是 RuntimeException 或 Error 的实例时,才按原样传播 throwable。 |
7 | static <X extends Throwable> void spreadIfPossible(Throwable throwable, Class<X>clarifiedType) 当且仅当它是 RuntimeException、Error 或 declaredType 的实例时,才按原样传播 throwable。 |
8 | static <X1 扩展 Throwable,X2 扩展 Throwable>voidpropagateIfPossible(Throwable throwable,Class<X1> 声明类型 1,Class<X2> 声明类型 2) 当且仅当它是 RuntimeException、Error、declaredType1 或 declaredType2 的实例时,才按原样传播 throwable。 |
继承的方法
该类继承了以下类的方法 -
- 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
查看结果。
InvalidInputException java.lang.ArrayIndexOutOfBoundsException: 4 at GuavaTester.getValue(GuavaTester.java:52) at GuavaTester.showcaseThrowables1(GuavaTester.java:38) at GuavaTester.main(GuavaTester.java:19)