Sunday, January 11, 2015

Java Exception Handling

An Exception is nothing but a problem which will occur during execution of program. An Exception can occur for many reasons. Below are the some of reasons,

  1. Invalid data from User
  2. Program try to read a file, but file is not available
  3. Program try to connect to Database, but credential is invalid

Exception Handling

Exception Handling is nothing but handle the Exception to avoid the abnormal termination of program. If an Exception is not handled, program will terminate abnormally.

To know the clear idea about an Exception handling, we will see the three categories of an Exceptions

  1. Checked Exceptions: Checked Exception is an Exception which can occur typically by user error. So programmer will not knowing it affront. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These Exceptions cannot be ignored by compiler. These kind of Exceptions are called "Checked Exception"
  2. Runtime Exceptions: Runtime Exception is an Exception which programmer can be knowing it affront based on critically of the code. Runtime Exceptions can be avoided during the development itself. But programmer is the responsible for that.
  3. Errors: There are not at all an Exceptions.The problem arise at beyond the user or programmer control. For example, JVM is running out of memory, this cannot be handled by program and cannot identify at compilation.

Exception Hierarchy

All Exceptions are subclasses of Exception class. Exception class is subclass of Throwable class. There is one more class which is derived from Throwable class is Error.


Catching an Exception

Exception handling is the combination of "try" and "catch" block. "try" will have the critical code which may arise an Exception. "catch" block should be written immediately next to "try" block. Whenever Exception is occurring inside the "try" block, control will go to respective "catch" block and execute the "catch" block.

     try {
           //critical code
     } catch(ExceptionName e) {
           //Handling block
     }

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in critical code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

Single catch block - Sample

Following code is having an array with size 2, when try to add 3rd element, program will throws an Exception.

     try {
           int[] i = new int[2];
           i[0] = 1;
           i[1] = 2;
           i[2] = 3;
     } catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("Exception thrown :" + e);
     }

Multiple catch block - Syntax and Sample

Syntax
     try {
           // Critical code
     } catch(ExceptionType e1) {
           // catch block 1
     } catch(ExceptionType e2) {
           // catch block 2
     } catch(ExceptionType e3) {
           // catch block 3
     }
Sample
     try{
           file = new FileInputStream(fileName);
           x = (byte) file.read();
     } catch(IOException i) {
           i.printStackTrace();
     } catch(FileNotFoundException f) {
           f.printStackTrace();
     }

No comments:

Post a Comment