ExceptionUtils

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author ParanoidCAT
 */
public class ExceptionUtils {
    private ExceptionUtils() throws Exception {
        throw new Exception("no ExceptionUtils instance should be created");
    }

    /**
     * 获取异常的堆栈信息
     *
     * @param throwable 异常
     * @return 堆栈信息文本
     */
    public static String getStackTrace(Throwable throwable) {
        try (StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter, true)) {
            throwable.printStackTrace(printWriter);
            return stringWriter.getBuffer().toString();
        } catch (IOException e) {
            return "";
        }
    }
}

 

posted on 2020-09-19 10:39  纠结的猫  阅读(322)  评论(0)    收藏  举报

导航