Guava Throwables 类
Throwable 类,简化异常和错误的传播与检查
1、类声明
以下是 com.google.common.base.Throwables
类的声明:
@GwtCompatible(emulated=true) public final class Throwables extends Object
2、类方法
官方文档:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/base/Throwables.html
方法类型 | 方法描述 |
---|---|
static List<Throwable> | getCausalChain(Throwable throwable) 获取一个Throwable的原因链的列表. |
static <X extends Throwable>X | getCauseAs(Throwable throwable, Class<X> expectedCauseType) 返回throwable的原因,强制转换为expectedCauseType. |
static Throwable | getRootCause(Throwable throwable) 返回抛出的最里面的原因. |
static String | getStackTraceAsString(Throwable throwable) 返回包含toString()的结果字符串,随后完整抛出,递归的堆栈跟踪。 |
static List<StackTraceElement> | lazyStackTrace(Throwable throwable) 返回throwable的堆栈跟踪,可能在整个跟踪上提供较慢的迭代,但在部分跟踪上的迭代速度更快. |
static boolean | lazyStackTraceIsLazy() 返回lazyStackTrace(java.lang.Throwable)是否将使用其文档中描述的特殊实现。 |
static RuntimeException | propagate(Throwable throwable) 把throwable包装成RuntimeException,用该方法保证异常传递,抛出一个RuntimeException异常. |
static <X extends Throwable>void | propagateIfInstanceOf(@Nullable Throwable throwable, Class<X>declaredType) Throwable类型为X才抛出. |
static void | propagateIfPossible(@Nullable Throwable throwable) Throwable类型为Error或RuntimeException才抛出。 |
static <X extends Throwable>void | propagateIfPossible(@Nullable Throwable throwable, Class<X>declaredType) Throwable类型为X, Error或RuntimeException才抛出. |
static <X extends Throwable>void | throwIfInstanceOf(Throwable throwable, Class<X> declaredType) 如果是declaredType的实例,则抛出throwable. |
static void | throwIfUnchecked(Throwable throwable) 如果是RuntimeException或Error,则抛出throwable. |
3、测试类
import com.google.common.base.Throwables; import org.junit.Test; public class ThrowablesTests { @Test public void test1() { ThrowablesTests tester = new ThrowablesTests(); 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]; } class InvalidInputException extends Exception { } }
四、相关文章
未经允许请勿转载:程序喵 » Google Guava 快速入门 —— 【基础】异常传播 Throwables 类