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.

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.

Logging an Exception in Java

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.