异常类自定义封装

/**
 * 异常追踪器封装
 * @author Administrator
 *
 */
public class ExceptionTracker {
//创建字符输出流的初始化大小
private static final int init_Buffer=2048;
public static String tracker(Exception exception) {
//创建一个字符输出流
StringWriter writer=new StringWriter(init_Buffer);
//创建一打印流获取字符输出流数据
PrintWriter printWriter=new PrintWriter(writer);
exception.printStackTrace(printWriter);
return writer.toString();
}
public static void main(String[] args) {

Exception exception=new Exception("自定义异常");
String tracker = ExceptionTracker.tracker(exception);
System.err.println(tracker);
}

}

打印结果


你可能感兴趣的:(java工具类)