码迷,mamicode.com
首页 > 其他好文 > 详细

异常信息重定向及相关处理办法

时间:2015-02-02 00:36:38      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 

    public static String outputThrowable(Throwable t) throws FileNotFoundException{
        for (StackTraceElement ste:t.getStackTrace()) {
            System.out.println(ste.getMethodName());
        }
        t.printStackTrace(System.err);
        t.printStackTrace(System.out);
        t.printStackTrace(new PrintStream("exception.txt"));//redirect to file
        
        StringWriter sw=new StringWriter();
        PrintWriter pw=new PrintWriter(sw);
        
        try {
            t.printStackTrace(pw);//redirect to file
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            pw.close();
        }
        
        return sw.toString();
        
    }

Output:

main
java.lang.ArithmeticException: / by zero
    at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
    at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
    at exception.TestException.main(TestException.java:11)
java.lang.ArithmeticException: / by zero
    at exception.TestException.main(TestException.java:11)

 




 

异常信息重定向及相关处理办法

标签:

原文地址:http://www.cnblogs.com/softidea/p/4266560.html

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