• 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");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如果我们想获取到最底层的真实异常信息,也就是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);
        }
    
    • 1
    • 2
    • 3

    看到了吗,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;
                }
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

  • 相关阅读:
    第四章分类问题
    全景环视AVM标定
    结合ChatGPT和MINDSHOW自动生成PPT
    T1175计算两个日期之间的天数
    20231109-【尚硅谷】3小时Ajax入门到精通学习笔记
    网络编程 lesson5 IO多路复用
    Jboss JMXInvokerServlet-deserialization漏洞复现
    牛客网_HJ2_计算某字符出现次数
    Jmeter快速入门
    Excel-VBA 快速上手(十二、Like 比较的常见用法)
  • 原文地址:https://blog.csdn.net/Urbanears/article/details/134020449