欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 异常嵌套报错, 打印 源异常信息 异常链信息

程序员文章站 2022-03-08 16:46:33
...

异常信息太长, 尝试实现只打印源头的异常信息

    @Test
    public void show() {
        try {
            try {
                try {
                    System.out.println("测试异常打印");
                    int i = 0;
                    i = 1 / i;

                } catch (Exception e) {
                    throw new RuntimeException("出现除0异常", e);
                }
            } catch (Exception e) {
                throw new Exception("通用异常处理", e);
            }
        } catch (Exception e) {

            // String stb = printSimpleLog(e); // 打印异常源头
            String stb = printSimpleLogLink(e);// 打印异常链路
            System.out.println(stb);

        }
    }

    /**
     * <pre>打印异常信息, 模拟 e.printStackTrace();</pre>
     */
    private String printSimpleLogLink(Throwable e) {

        List<Throwable> list = Lists.newArrayList();
        getOriginExceptionLink(e, list);

        StringBuilder stb = new StringBuilder();
        for (Throwable throwable : list) {
            String str = printSimpleLog(throwable);
            stb.append(str);
        }
        return stb.toString();
    }

    /**
     * <pre>打印异常信息的链路</pre>
     */
    private String printSimpleLog(Throwable e) {
        String stackTraceStr = getStackTrace(e);
        StringBuilder stb = new StringBuilder();
        stb.append("Caused by: ");
        stb.append(e);
        stb.append("\n\tat ");
        stb.append(stackTraceStr);
        stb.append("\n");
        return stb.toString();
    }

    /**
     * 获取异常链
     */
    Throwable getOriginExceptionLink(Throwable e, List<Throwable> list) {
        Throwable cause = e.getCause();
        if (cause == null) {
            list.add(e);
            return e;
        }

        if (cause.equals(e)) {
            return e;
        } else {
            list.add(e);
            return getOriginExceptionLink(cause, list);
        }
    }

    /**
     * 获取异常源头
     */
    Throwable getOriginException(Throwable e) {
        Throwable cause = e.getCause();
        if (cause == null) {
            return e;
        }

        if (cause.equals(e)) {
            return e;
        } else {
            return getOriginException(cause);
        }
    }

    /**
     * 取堆栈信息
     */
    public String getStackTrace(Throwable e) {
        // 取堆栈信息对象
        StackTraceElement stackTraceElement = e.getStackTrace()[0];

        String className = stackTraceElement.getClassName();// 报错的报名+类名
        // Class<? extends StackTraceElement> aClass = stackTraceElement.getClass(); //堆栈跟踪对象 class java.lang.StackTraceElement
        String fileName = stackTraceElement.getFileName(); // 报错的文件名+后缀
        int lineNumber = stackTraceElement.getLineNumber(); // 报错的行号
        String methodName = stackTraceElement.getMethodName(); // 报错的 方法名
        // System.out.println(className);
        // System.out.println(fileName);
        // System.out.println(lineNumber);
        // System.out.println(methodName);
        StringBuilder stb = new StringBuilder();
        stb.append(className).append(".").append(methodName).//
                append("(").append(fileName).append(":").append(lineNumber).append(")");

        return stb.toString();
    }

输出 异常源头

Caused by: java.lang.ArithmeticException: / by zero
>  at  cn.Test.show(Test.java:78)

输出 异常链

Caused by: java.lang.Exception: 通用异常处理
>  at cn.Test.show(Test.java:85)
Caused by: java.lang.RuntimeException: 出现除0异常
>  at cn.Test.show(Test.java:82)
Caused by: java.lang.ArithmeticException: / by zero
>  at cn.Test.show(Test.java:79)