Guava:Throwables 异常工具

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>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.

测试Demo

java 复制代码
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);
        }
    }
}
相关推荐
Mr.Entropy7 小时前
ecplise配置maven插件
java·maven
叙白冲冲8 小时前
tomcat 为啥能一直运行?不像方法那样结束?
java·tomcat
CoderYanger8 小时前
MySQL数据库——3.2.1 表的增删查改-查询部分(全列+指定列+去重)
java·开发语言·数据库·mysql·面试·职场和发展
迷知悟道8 小时前
java面向对象四大核心特征之抽象---超详细(保姆级)
java·后端
PEI049 小时前
MVCC(多版本并发控制)
java·开发语言·数据库
半夏陌离10 小时前
SQL 实战指南:电商订单数据分析(订单 / 用户 / 商品表关联 + 统计需求)
java·大数据·前端
我真的是大笨蛋10 小时前
K8S-Pod(上)
java·云原生·容器·kubernetes
纪元A梦10 小时前
贪心算法应用:数字孪生同步问题详解
java·算法·贪心算法
Micrle_00710 小时前
java分布式场景怎么实现一个高效的 读-写锁
java·分布式