// exceptions/LoggingExceptions.java // (c)2020 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // An exception that reports through a Logger // {ErrorOutputExpected} import java.util.logging.*; import java.io.*; class LoggingException extends Exception { private static Logger logger = Logger.getLogger("LoggingException"); 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: ___[ Error Output ]___ May 09, 2017 6:07:17 AM LoggingException SEVERE: LoggingException at LoggingExceptions.main(LoggingExceptions.java:20) Caught LoggingException May 09, 2017 6:07:17 AM LoggingException SEVERE: LoggingException at LoggingExceptions.main(LoggingExceptions.java:25) Caught LoggingException */