在开发和调试程序的时候,如果发生异常可以调用异常的printStackTrace()方法将异常打印在控制台上,或者使用日志框架记录异常信息,但有时我们希望可以将用户操作时出现的异常记录到数据库中(现在我就碰到这样的需求),然后可以根据特定的条件查询出异常进行分析,比如像Eclipse的ErrorLog视图那样显示错误日志,然后双击显示异常详细信息,或者像下面我的项目中这样,通过下面简单的方式可以将错误堆栈信息输出到文本框中.
下面是Java代码:
try
{
Exception exception = messageErrorLog.getException();
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
errorLogDetailText.setText(writer.getBuffer().toString());
}finally {
if(writer != null )
try {
writer.close();
}catch (Exception e) {
e.printStackTrace();
}
}
Exception exception = messageErrorLog.getException();
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
errorLogDetailText.setText(writer.getBuffer().toString());
}finally {
if(writer != null )
try {
writer.close();
}catch (Exception e) {
e.printStackTrace();
}
}