服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

- redirect printstacktrace() to a string

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 "------" + sw.tostring() + "------";
     }
   catch(exception e2) {
     return "bad stack2string";
     }
   }
}

扫描关注微信公众号