package com.think_in_java_Chapter12;
import java.io.*;
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 );
}
}
}
output:
五月 02, 2016 9:09:02 下午 com.think_in_java_Chapter12.LoggingException
严重: com.think_in_java_Chapter12.LoggingException
at com.think_in_java_Chapter12.LoggingExceptions.main(LoggingExceptions.java:17)
Caught com.think_in_java_Chapter12.LoggingException
五月 02, 2016 9:09:02 下午 com.think_in_java_Chapter12.LoggingException
严重: com.think_in_java_Chapter12.LoggingException
at com.think_in_java_Chapter12.LoggingExceptions.main(LoggingExceptions.java:22)
Caught com.think_in_java_Chapter12.LoggingException