首页 > 其他 > 详细

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

时间:2015-02-02 00:33:20      阅读:324      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!