当前位置:首页>网络学院>程序开发>JAVA教程>文章内容

- Redirect printStackTrace() to a String

[ 来源:www.it55.com | 作者: | 时间:2007-07-19 | 收藏 | 推荐 ] 【

import java.io.*;

public class TestStack2String {
  public static void main(String s[]){
   try {
     // force an exception for demonstration purpose
     Class.forName("unknown").newInstance();
     }
   catch (Exception e) {
     System.out.println(stack2string(e));
     }
   }

  static public String stack2string(Exception e) {
   try {
     StringWriter sw = new StringWriter();
     PrintWriter pw = new PrintWriter(sw); sflj www.it55.com kg^&fgd
     e.printStackTrace(pw);
     return "------" + sw.toString() + "------";
     }
   catch(Exception e2) {
     return "bad stack2string";
     }
   }
}

(编辑:IT资讯之家 www.it55.com

返回顶部
 

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]