Friday, 31 May 2013

5. Exceptions in JAVA

finally Keyword:

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Using a finally block allows you to run any cleanup-type statements that you want to execute(like closing JDBC statements), no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following syntax:

try
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}finally
{
   //The finally block always executes.
}

Example:

public class FinallyExample1 {

/**
 * @param args
 */
public static void main(String[] args) {
int[] a = new int[] { 9, 8, 7 };
try {
/**
 * Here we have array a of size 3. But in for loop we are trying to
 * access 4th element of an array(for loop continues 4 times from i
 * value 0 to 3). so here we got exception array out of bounds
 * exception.
 */
for (int i = 0; i < 4; i++) {
System.out.println("element at index " + i + " is " + a[i]);
}
catch (ArrayIndexOutOfBoundsException e) {
/**
 * Here we know may got ArrayIndexOutOfBoundsException so we have
 * used exception of type ArrayIndexOutOfBoundsException in catch
 * block. If we do not know what type of exception we may got then
 * we should use Exception. Because exception is superclass for all
 * exception types.
 */
e.printStackTrace();
finally {
/**
 * Finally block always called.
 */
System.out.println("Finally Block call whether we got exception or not");
}
}
}

OutPut:
element at index 0 is 9
element at index 1 is 8
element at index 2 is 7
java.lang.ArrayIndexOutOfBoundsException: 3
at com.sample.javase.testing.FinallyExample.main(FinallyExample.java:18)
Finally Block call whether we got exception or not

try block must be followed by either catch or finally block. So above example can be rewritten as follows:

public class FinallyExample2 {

/**
 * @param args
 */
public static void main(String[] args) {
 
      int[] a = new int[] { 9, 8, 7 };

try {
/**
 * Here we have array a of size 3. But in for loop we are trying to
 * access 4th element of an array(for loop continues 4 times from i
 * value 0 to 3). so here we got exception array out of bounds
 * exception.
 */
for (int i = 0; i < 4; i++) {
System.out.println("element at index " + i + " is " + a[i]);
}
finally {
/**
 * Finally block always called.
 */
System.out.println("It did call whether we got exception or not");
}
}
}

OutPut:
element at index 0 is 9
element at index 1 is 8
element at index 2 is 7
Finally Block call whether we got exception or not
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at com.sample.javase.testing.FinallyExample.main(FinallyExample.java:18)

Note: Differences between above two examples is "exception is caught first and then calls the finally block as we used catch block in example1 but in example 2,  first finally block calls and then it caught the exception as we are not using catch block".

No comments:

Post a Comment