Guava:Throwables 异常工具

34 篇文章 0 订阅
11 篇文章 0 订阅
本文详细介绍了Guava库中的GuavaThrowables类,包括其提供的各种方法,如获取异常原因链、强制转换异常类型、获取根因、堆栈跟踪等,以及在实际测试中的应用示例。
摘要由CSDN通过智能技术生成

Guava Throwables 类

Throwable 类,简化异常和错误的传播与检查

类方法说明

官方文档:Throwables (Guava: Google Core Libraries for Java 27.0.1-jre API)

方法类型方法描述
static List<Throwable>getCausalChain(Throwable throwable) 
获取一个Throwable的原因链的列表.
static <X extends Throwable>XgetCauseAs(Throwable throwable, Class<X> expectedCauseType) 
返回throwable的原因,强制转换为expectedCauseType.
static ThrowablegetRootCause(Throwable throwable) 
返回抛出的最里面的原因.
static StringgetStackTraceAsString(Throwable throwable) 
返回包含toString()的结果字符串,随后完整抛出,递归的堆栈跟踪。
static List<StackTraceElement>lazyStackTrace(Throwable throwable) 
返回throwable的堆栈跟踪,可能在整个跟踪上提供较慢的迭代,但在部分跟踪上的迭代速度更快.
static booleanlazyStackTraceIsLazy() 
返回lazyStackTrace(java.lang.Throwable)是否将使用其文档中描述的特殊实现。
static RuntimeExceptionpropagate(Throwable throwable) 
把throwable包装成RuntimeException,用该方法保证异常传递,抛出一个RuntimeException异常.
static <X extends Throwable>voidpropagateIfInstanceOf(@Nullable Throwable throwable, Class<X>declaredType) 
Throwable类型为X才抛出.
static voidpropagateIfPossible(@Nullable Throwable throwable) 
Throwable类型为Error或RuntimeException才抛出。
static <X extends Throwable>voidpropagateIfPossible(@Nullable Throwable throwable, Class<X>declaredType) 
Throwable类型为X, Error或RuntimeException才抛出.
static <X extends Throwable>voidthrowIfInstanceOf(Throwable throwable, Class<X> declaredType) 
如果是declaredType的实例,则抛出throwable.
static voidthrowIfUnchecked(Throwable throwable) 
如果是RuntimeException或Error,则抛出throwable.

测试Demo

import com.google.common.base.Throwables;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class ThrowablesTests {

    public void getException(){
        int[] data = {1, 2, 3};
        int datum = data[4];
    }

    @Test
    public void test1() {
        try {
            getException();
        } catch (Exception e) {
            List<Throwable> causalChain = Throwables.getCausalChain(e);
            System.out.println(causalChain);
        }
    }

    @Test
    public void test2() {
        try {
            // 模拟抛出一个异常
            throw new IOException("An I/O error occurred");
        } catch (Exception e) {
            // 获取异常的原因,并检查是否为 IOException 类型或其子类型
            Throwable cause = Throwables.getCauseAs(e, IOException.class);
            if (cause != null) {
                System.out.println("Cause is an IOException: " + cause.getMessage());
            } else {
                System.out.println("Cause is not an IOException");
            }
        }
    }

    @Test
    public void test3() {
        try {
            // 模拟抛出一个嵌套多层的异常
            throw new IOException("An I/O error occurred");
        } catch (Exception e) {
            Throwable rootCause = Throwables.getRootCause(e);
            String stackTraceAsString = Throwables.getStackTraceAsString(rootCause);
            System.out.println(stackTraceAsString);
        }
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值