关于异常抛出的问题

钟伟海 2005-07-19 03:12:28
在try{

}
catch(Exception e){}中,用e.printStackTrace();可以在控制台打出显示下异常情况,现在我想把异常信息保存到一个文本文件中去,写入文本没问题,但如何能得到和控制台显示的一样的信息保存进文本里呢?

e.getStackTrace()
e.getMessage()等 只能得到一个简单的错误提示,如:java.lang.NullPointerException,没有e.printStackTrace();详细。
...全文
215 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
YuLimin 2005-07-24
  • 打赏
  • 举报
回复
用Log4J或Commons-Logging吧
humanity 2005-07-23
  • 打赏
  • 举报
回复
System.setOut(new PrintStream(new FileStream(new FileOutputStream("sysout.log")));
System.setErr(new PrintStream(new FileStream(new FileOutputStream("syserr.log")));

interhanchi 2005-07-22
  • 打赏
  • 举报
回复
printStackTrace

public void printStackTrace(PrintStream s)
Prints this throwable and its backtrace to the specified print stream.

Parameters:

s - PrintStream to use for output
congliu 2005-07-22
  • 打赏
  • 举报
回复
e.printStackTrace(new PrintWriter(File Name));
westwin 2005-07-21
  • 打赏
  • 举报
回复
一楼正解
mofeir 2005-07-21
  • 打赏
  • 举报
回复
用Logger吧,呵呵。
simbas00 2005-07-19
  • 打赏
  • 举报
回复
定义自己的异常类
bluesmile979 2005-07-19
  • 打赏
  • 举报
回复
e.printStackTrace(new PrintWriter(File Name));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧