/**
* 书本:《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