|
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.
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);
}
}
|
Here is a sample of the output generated by the example:
Jan 11, 2002 5:16:49 PM com.library.Books Method
SEVERE: Uncaught exception
java.io.IOException
at com.library.Books.Method(com.library.Books.java:32)
at com.library.Books.main(com.library.Books.java:18)
Jan 11, 2002 5:16:50 PM com.library.Books Method
FINER: THROW
java.lang.IllegalStateException
at com.library.Books.Method(com.library.Books.java:25)
at com.library.Books.main(com.library.Books.java:18)
|
Related Tips
|
On our system, for certain exceptions like NullPointerException we had to do something a little more exotic to get the stack trace...