2 qq 25871975 qq_25871975 于 2016.05.02 21:10 提问

能解释下下面这段代码的含义,以及为什么会这样输出吗?(没有金币了,拜托了)

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

1个回答

caozhy
caozhy   Ds   Rxr 2016.05.02 23:32

你的程序自己故意丢出异常,然后自己捕获并且输出了下。Log类负责输出出来。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!