dangsn
2024-11-14 6284dcac25ffb84fa2f6a9007a36655856e24e46
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.vci.common.exception;
 
public class VciExceptionTool {
 
    /**
     * 获取异常的首行显示信息
     * @param e
     * @return
     */
    public static String getExceptionStr(Throwable e) {
        if (e == null) {
            return "";
        }
        String str = "";
        if (e.getCause() != null) {
            str = e.getCause().toString();
        } else {
            str = e.toString();
        }
        
        if (str == null) {
            str = "";
        }
        return str;
    }
    
    /**
     * 获取异常的所有信息
     * @param e
     * @return
     */
    public static String getExceptionDetail(Throwable e) {
        if (e == null) {
            return "";
        }
        StringBuffer msg = new StringBuffer("");
        msg.append(getStackTraceDetail(e));
        if (e.getCause() != null) {
            msg.append(getStackTraceDetail(e.getCause()));
        }
        return msg.toString();
    }
    
    private static String getStackTraceDetail(Throwable e) {
        StringBuffer msg = new StringBuffer("");
        String message = e.toString();
        int length = e.getStackTrace().length;
        if (length > 0) {
            msg.append(message + "\n");
            for (int i = 0; i < length; i++) {
                msg.append("\t" + e.getStackTrace()[i] + "\n");
            }
            
        } else {
            msg.append(message);
        }
        
        return msg.toString();
    }
}