问题描述
- 能解释下下面这段代码的含义,以及为什么会这样输出吗?(没有金币了,拜托了)
-
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
解决方案
你的程序自己故意丢出异常,然后自己捕获并且输出了下。Log类负责输出出来。