java将异常输出到日志_【ThinkingInJava】25、将异常输出记录到日志
/**
* 書本:《Thinking In Java》
* 功能:將異常輸出記錄到日志中。
* 文件:LoggingExceptions.java
* 時間:2015年4月8日21:11:51
* 作者:cutter_point
*/
package Lesson12_error_handling_with_exceptions;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;
class LoggingException extends Exception
{
private static Logger logger = Logger.getLogger("LoggingException");
public LoggingException()
{
StringWriter trace = new StringWriter();
printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
}
public class LoggingExceptions
{
public static void main(String [] args)
{
try
{
throw new LoggingException();
}
catch (LoggingException e)
{
System.err.println("Caught " + e);
}
try
{
throw new LoggingException();
}
catch (LoggingException e)
{
System.err.println("Caught " + e);
}
}
}
輸出:
五月 04, 2015 3:53:49 下午 Lesson12_error_handling_with_exceptions.LoggingException 嚴重: Lesson12_error_handling_with_exceptions.LoggingException ?? ?at Lesson12_error_handling_with_exceptions.LoggingExceptions.main(LoggingExceptions.java:32) Caught Lesson12_error_handling_with_exceptions.LoggingException 五月 04, 2015 3:53:49 下午 Lesson12_error_handling_with_exceptions.LoggingException 嚴重: Lesson12_error_handling_with_exceptions.LoggingException ?? ?at Lesson12_error_handling_with_exceptions.LoggingExceptions.main(LoggingExceptions.java:41) Caught Lesson12_error_handling_with_exceptions.LoggingException
總結
以上是生活随笔為你收集整理的java将异常输出到日志_【ThinkingInJava】25、将异常输出记录到日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机连接蓝牙扫码枪_宝马车与手机无法蓝牙
- 下一篇: STP的选举过程