Java中获取异常栈中的底层异常信息-分析Java异常栈

Java中获取异常栈中的底层异常信息-分析Java异常栈

首先,我们准备好一个多层异常栈堆叠的示例代码:

public class ExceptionUtils {
    public static void main(String[] args) {
        try {
            buildMultiLayerExceptionStack();
        }  catch (Exception e) {
            e.printStackTrace();

            // 获取最底层的堆栈异常信息
            Throwable throwable = e;
            while (throwable != null) {
                Throwable nextCause = throwable.getCause();
                if (nextCause == null) {
                    System.out.println("root exception message: " + throwable.getMessage());
                    break;
                }
                throwable = nextCause;
            }

        }
    }

    public static void buildMultiLayerExceptionStack() throws Exception {
        try {
            method1();
        } catch (Exception e) {
            throw new Exception("Exception in method1", e);
        }
    }

    public static void method1() throws Exception {
        try {
            // some code that may throw an exception
            method2();
        } catch (Exception e) {
            throw new Exception("Exception in method2", e);
        }
    }

    public static void method2() throws Exception {
        // some code that may throw an exception
        throw new Exception("Stack bottom exception message");
    }
}

buildMultiLayerExceptionStack方法中,我们构造了一个多层嵌套的Exception异常信息栈,得到的异常栈结构如下所示:

java.lang.Exception: Exception in method1
	at com.netease.mail.data.test.ExceptionUtils.buildMultiLayerExceptionStack(ExceptionUtils.java:35)
	at com.netease.mail.data.test.ExceptionUtils.main(ExceptionUtils.java:12)
Caused by: java.lang.Exception: Exception in method2
	at com.netease.mail.data.test.ExceptionUtils.method1(ExceptionUtils.java:44)
	at com.netease.mail.data.test.ExceptionUtils.buildMultiLayerExceptionStack(ExceptionUtils.java:33)
	... 1 more
Caused by: java.lang.Exception: Stack bottom exception message
	at com.netease.mail.data.test.ExceptionUtils.method2(ExceptionUtils.java:50)
	at com.netease.mail.data.test.ExceptionUtils.method1(ExceptionUtils.java:42)
	... 2 more

如果我们想获取到最底层的真实异常信息,也就是Caused by: java.lang.Exception: Stack bottom exception message 这条异常信息,来提示告警下发,要怎么做呢?

首先在debug模式下,我们先观察一下这个Exception的异常栈结构:

在这里插入图片描述

打开后可以观察到,栈底异常信息的cause属性似乎是在不断的嵌套重复。那此时,如果不停的调用e.getCause来获取每个异常的Cause属性的话,岂不是不会结束了吗?

针对这个问题,我们来看下e.getCause的源码:

    public synchronized Throwable getCause() {
        return (cause==this ? null : cause);
    }

看到了吗,java底层本身就给了我们解决方法,在调用e.getCause方法时,会判断cause属性与当前的Throwable异常是否为同一对象,如果是则会返回null。

因此,我们只需要在不停调用e.getCause来层层递进获取栈底异常信息时,判断是否得到的Cause为Null,即可判断是否已经调用到了异常栈的栈底。

有了上面的结论,我们就可以得到下面的代码来获取异常栈的栈底异常信息:

        try {
            buildMultiLayerExceptionStack();
        }  catch (Exception e) {
            e.printStackTrace();

            // 获取最底层的堆栈异常信息
            Throwable throwable = e;
            while (throwable != null) {
                Throwable nextCause = throwable.getCause();
                if (nextCause == null) {
                    System.out.println("root exception message: " + throwable.getMessage());
                    break;
                }
                throwable = nextCause;
            }

        }
    }

上面的代码中,利用while循环对异常栈进行遍历,调用throwable.getCause()来获取每个异常栈的上一层异常信息(也就是造成当前异常的Cause)。直到判断得到的上一层异常为Null,说明已经遍历到了异常栈的栈底,从而获取到栈底的最开始的异常信息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JermeryBesian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值