This Java tip illustrates an example of logging an Exception. Java provides log() method in Logger class to log an exception. Further, throwing() is used to throw an exception.


Here is a sample of the output generated by the example:

  Jan 11, 2002 5:16:49 PM com.library.Books Method
  package com.library;

    class Books {

        public void myMethod() {

            Logger log = Logger.getLogger("com.library.Books");
            // In case an exception is encounted
            try {
                // Test with an exception
                throw new IOException();
            } catch (Throwable e){
                // Log the exception
                log.log(Level.SEVERE, "Uncaught exception", e);
            // In case the method is throwing an exception
            Exception ex = new IllegalStateException();
            log.throwing(this.getClass().getName(), "Method", ex);
  SEVERE: Uncaught exception
            at com.library.Books.Method(
            at com.library.Books.main(
    Jan 11, 2002 5:16:50 PM com.library.Books Method
            at com.library.Books.Method(
            at com.library.Books.main(