码迷,mamicode.com
首页 > 编程语言 > 详细

Java异常堆栈字符串输出

时间:2018-06-14 20:52:23      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:tostring   get   str   rac   string   ram   信息   输出   print   

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异常堆栈字符串输出

标签:tostring   get   str   rac   string   ram   信息   输出   print   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!