永发信息网

怎么获取e.printStackTrace的内容并记录到log4j日志中

答案:2  悬赏:20  手机版
解决时间 2021-03-20 10:46
  • 提问者网友:流星是天使的眼泪
  • 2021-03-19 21:25
怎么获取e.printStackTrace的内容并记录到log4j日志中
最佳答案
  • 五星知识达人网友:夜余生
  • 2021-03-19 21:42
源代码如下:已测试成功
import java.io.*;
public class ThrowableTest extends FileNotFoundException {
public static void main(String argv[]) {
try {
FileInputStream f = new FileInputStream("D:\\test.txt");
} catch (FileNotFoundException e) {
try {
PrintWriter p = new PrintWriter(new FileOutputStream("D:\\errors.txt"));
p.println("=== toString() ===");
p.println(e.toString()+"\n");
p.println("=== getLocalizedMessage() ===");
p.println(e.getLocalizedMessage());
p.println("=== getMessage() ===\n");
p.println(e.getMessage());
p.println("=== printStackTrace() ===");
e.printStackTrace(p);
p.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
全部回答
  • 1楼网友:愁杀梦里人
  • 2021-03-19 22:06
不用重写,public void printstacktrace(printwriter s),java本来有这个方法。 源代码如下:已测试成功 import java.io.*; public class throwabletest extends filenotfoundexception { public static void main(string argv[]) { try { fileinputstream f = new fileinputstream("d:\\test.txt"); } catch (filenotfoundexception e) { try { printwriter p = new printwriter(new fileoutputstream("d:\\errors.txt")); p.println("=== tostring() ==="); p.println(e.tostring()+"\n"); p.println("=== getlocalizedmessage() ==="); p.println(e.getlocalizedmessage()); p.println("=== getmessage() ===\n"); p.println(e.getmessage()); p.println("=== printstacktrace() ==="); e.printstacktrace(p); p.flush(); } catch (ioexception e1) { e1.printstacktrace(); } } } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯