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.


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Ads
Share on Google+Share on Google+

Logging an Exception in Java

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

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()?

Ads

 

Ads