首页 > 编程语言 > 详细

Java异常堆栈字符串输出

时间:2018-06-14 20:32:28      阅读:184      评论:0      收藏:0      [点我收藏+]
Java异常堆栈字符串输出
public class ExceptionTrans {
    /**
     * 异常信息转换为字符串
     *
     * @param t 异常对象
     * @return
     */
    public static String ex2String(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }

    public static void main(String[] args) {
        String ex = null;
        try {
            int a = 0;
            a = a / a;
            if (true) {
                throw new CordException("test");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ex = ex2String(e);
            System.out.println("------------------------");
        }
        System.out.printf(ex);
    }
}

Java异常堆栈字符串输出

原文:http://blog.51cto.com/lavasoft/2129490

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!