Monday 25 March 2013

How to handle an exception?

Let us continue with the above example and see how we can handle an exception. We should enclose the block of lines where the exception can occur in a try block and followed by that we should have a catch block. In catch(…) we should specify the type of exception which we intend to handle. The exception class that is specified in catch block will handle exceptions including all its sub types. That is, if we specify RuntimeException in catch, it will catch IndexOutOfBoundsException and also ArrayIndexOutOfBoundsException too. That is, it will catch RuntimeException and all its sub types. Refer the above image for relationship hierarchy. So, if we put Exception is catch block it will handle all possible java exceptions. Only one catch block is sufficient forever. This is technically correct but it is not a best practice. We should catch the lowest possible exception in the hierarchy. Instead putting RuntimeException, we should have ArrayIndexOutOfBoundsException in catch block.
public class ExceptionStack {
    public static void main(String args[]) {
  int value = get(10);
  System.out.println(value);
    }
 
    public static int get(int location) {
  int[] tempArr = new int[5];
  int result = 0;
  try {
    result = tempArr[location];
  } catch(ArrayIndexOutOfBoundsException exception) {
    exception.printStackTrace();
  }
  return result;
    }
}
What have we done in the above example source code? We have just enclosed the code block where an exception can occur using try and a catch block. In catch block we say that, we intend to handle ArrayIndexOutOfBoundsException and we print the trace. I have purposefully added theexception.printStackTrace(); line in the catch block. If we run this code listing the output we get is exactly the same as the one we got in the previous example. We have handled the exception but have done nothing. One more best practice is, just logging an exception and doing nothing is not good (unless otherwise we don’t have anything else to do, just the program execution needs to continue case).
public class ExceptionStack {
    public static void main(String args[]) {
  int value = get(10);
  System.out.println(value);
    }
 
    public static int get(int location) {
  int[] tempArr = new int[5];
  int result = 0;
  try {
    result = tempArr[location];
  } catch(ArrayIndexOutOfBoundsException exception) {
        System.out.println("No such element and so setting zero");
    result = 0;
  }
  return result;
    }
}
In the above example, we have handled the exception in an appropriate way. We have logged the message then returned 0 and continuing with program flow. This is just an example, the business may not warrant to return 0 and in that case we cannot use this. This is just an example to show meaningful handling of an exception. If it is all about logging and proceeding with the flow, even an if-block can do the job and no exception handling is required. So, use exception handling judiciously. There should be a meaningful purpose to it when used.

No comments:

Post a Comment