CSDN博客

img wxyxl

- Redirect printStackTrace() to a String

发表于2001/5/4 14:17:00  663人阅读


 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);
     e.printStackTrace(pw);
     return "------/r/n" + sw.toString() + "------/r/n";
     }
   catch(Exception e2) {
     return "bad stack2string";
     }
   }
}

0 0

相关博文

我的热门文章

img
取 消
img