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);
        }
    }
}
相关推荐
顽疲5 分钟前
从零用java实现 小红书 springboot vue uniapp(13)模仿抖音视频切换
java·vue.js·spring boot
星辰离彬26 分钟前
Java 与 MySQL 性能优化:MySQL连接池参数优化与性能提升
java·服务器·数据库·后端·mysql·性能优化
半桔26 分钟前
【Linux手册】从接口到管理:Linux文件系统的核心操作指南
android·java·linux·开发语言·面试·系统架构
nightunderblackcat35 分钟前
新手向:实现ATM模拟系统
java·开发语言·spring boot·spring cloud·tomcat·maven·intellij-idea
Bug退退退12340 分钟前
RabbitMQ 高级特性之延迟队列
java·spring·rabbitmq·java-rabbitmq
先睡44 分钟前
RabbitMQ
java
笑衬人心。1 小时前
Java 17 新特性笔记
java·开发语言·笔记
麦兜*2 小时前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
ruan1145143 小时前
MySQL4种隔离级别
java·开发语言·mysql