Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Util Logging an Exception in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Logging an Exception in Java

Advertisement
This section introduces you the concept of logging exception handling. When certain conditions are not followed or wrong then compiler shows a message which is mentioned under the specific exception.

Logging an Exception in Java

     

This section introduces you the concept of logging exception handling. When certain conditions are not followed or wrong then compiler shows a message which is mentioned under the specific exception. Exception is an event that occurred when the program is execute or in running time. This is used for the error handling. There are several errors are caught by different specific exceptions like the "divided by zero" error is under the ArithmeticException, blank array is under the NullPointerException, blank stack is a EmptyStackException and many others errors are handled by many exceptions like that.

Description of program:
This program represents two exceptions like: ArithmeticException and NullPointerException by Logger object. Program contains two method DivByZero() method and ArrayBound() method which provides an exception at the running time of program. The DivByZero method throws ArithmethicException because '1' is not divisible by '0' and another method gives the working illustration of NullPointerException that catch the error when the array is blank. 

Description of code:

log(Level level, String message, Throwable thrown):
Above method shows a log message generated by Throwable information to the system. It takes level provided by logger and also a string type value with given message and throwable information at the time of running.

Here is the code of program:

import java.io.*;
import java.util.logging.*;

public class LogException{
  public static void main(String[] args) {
  LogException l = new LogException();
  }
  public void DivByZero() {
  System.out.println(1/0);
  }
  int arr[];
  public void ArrayBound() {
  System.out.println(arr[0]);
  }
  public LogException(){
  Logger log = Logger.getLogger("");
  try{
  DivByZero();
  }
  catch (Exception e){
  log.log(Level.WARNING, "Can not divede by zero", e.getMessage());
  }
  try{
  ArrayBound();
  }
  catch(Exception ex){
  log.log(Level.INFO, "Array is blank", ex.getMessage());
  }
  }
}

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2008

Related Tutorials

Discuss: Logging an Exception in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Nigel Thomas
December 20, 2012
Logging exception

In the example given above, if we look it into the last line of code : log.log(Level.INFO, "Array is blank", ex.getMessage()); we can see ex.getMessage() is being used. Why can't we use ex only while passing into the log()?
DMCA.com