java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4089
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to log an Exception E-mail
User Rating: / 22
PoorBest 

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 112002 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 112002 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

 
< Prev   Next >
Posted by Chris, on Wednesday, 07 June 2006 at 11:47

On our system, for certain exceptions like NullPointerException we had to do something a little more exotic to get the stack trace...

  private String getStackTraceForException(Exception e) {        StringWriter sw = new StringWriter(2000);    PrintWriter pw = new PrintWriter(sw);    e.printStackTrace(pw);    return sw.getBuffer().toString(); }


 1 
Page 1 of 1 ( 1 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.