/*** Book: Thinking in Java * Function: Log the exception output to the journal. * File: loggingexceptions.java* time: April 8, 2015 21:11:51* Author: 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");p ublic Loggingexception () {StringWriter trace = new StringWriter ();p rintstacktrace (new PrintWriter (trace)); Logger.severe ( Trace.tostring ());}} public class Loggingexceptions{public static void Main (String [] args) {try {throw new loggingexception ();} catch (Logginge Xception e) {System.err.println ("caught" + e);} Try {throw new loggingexception ();} catch (Loggingexception e) {System.err.println ("caught" + E);}}}
Output:
May 04, 2015 3:53:49 pm lesson12_error_handling_with_exceptions. Loggingexception <init>
Serious: lesson12_error_handling_with_exceptions. Loggingexception
At Lesson12_error_handling_with_exceptions. Loggingexceptions.main (LOGGINGEXCEPTIONS.JAVA:32)
Caught lesson12_error_handling_with_exceptions. Loggingexception
May 04, 2015 3:53:49 pm lesson12_error_handling_with_exceptions. Loggingexception <init>
Serious: lesson12_error_handling_with_exceptions. Loggingexception
At Lesson12_error_handling_with_exceptions. Loggingexceptions.main (loggingexceptions.java:41)
Caught lesson12_error_handling_with_exceptions. Loggingexception
"Thinkinginjava" 25, log abnormal output to logs