houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.iailab.framework.common.exception;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
 
/**
 * Exception工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class ExceptionUtils {
 
    /**
     * 获取异常信息
     * @param ex  异常
     * @return    返回异常信息
     */
    public static String getErrorStackTrace(Exception ex){
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw, true);
            ex.printStackTrace(pw);
        }finally {
            try {
                if(pw != null) {
                    pw.close();
                }
            } catch (Exception e) {
 
            }
            try {
                if(sw != null) {
                    sw.close();
                }
            } catch (IOException e) {
 
            }
        }
 
        return sw.toString();
    }
}