Recent Posts

Saturday, 16 July 2016

Exception Handling Tutorial Part 2


Difference Between final, finally, finalize
final
     It is the modifier applicable for classes methods and variables. For final classes we can’t create child classes i.e inheritance is not possible.
      final() methods can’t be override in child classes for final variables reassignments is not possible because they are constants.

finally
     It is a block associated with try catch the main objective of finally block is to maintain cleanup code which should execute always.

finalize
     It is a method should be executed by the “Garbage Collector” just before destroying an object. The main objective of finalize method is to maintain cleanup code.

Note
     When compared with finalize, finally is always recommended to maintain cleanup code because there is no guarantee for the exact behavior of “Garbage Collector” it is Virtual Machine Dependent.

Control flow in try - catch – finally
The following program will demonstrates the control flow in different cases.
E.g
class ExceptionDemo {
   public static void main(String arg[]) {
      try {
         statement1;
         statement2;
         statement3;
      }
      catch (X e) {
         statement4;
      }
      finally {
         statement5;
      }
      statement6;
   }
}
Case 1: if there is no exception, then the statements 1, 2, 3, 5, 6 will execute with normal termination.

Case 2: if an exception raised at statement-2 and the corresponding catch block matched, then the statements 1, 4, 5, 6 will execute with normal termination.

Case 3: if an exception raised at statement-2 but the corresponding catch block not matched then the statements 1, 5, 6 will execute with abnormal termination.

Case 4: if an exception raised at statement-2 and while executing the corresponding catch block at statement–4 an exception raised then the statements 1, 5 will execute with abnormal termination.

Case 5: if an exception raised at statement-5 or at statement-6 then it is always abnormal condition. Control flow in nested try – catch – finally

Control flow in nested try – catch – finally
     The following program will demonstrates the flow of nested try –catch – finally
E.g
try {
   statement 1;
   statement 2;
   statement 3;
   try {
      statement 4;
      statement 5;
      statement 6;
   }
   catch (X e) {
      statement 7;
   }
   finally {
      statement 8;
   }
   statement 9;
}
catch (Y e) {
   statement 10;
}
finally {
   statement 11;
}
statement 12;
Case 1: if there is no exception then the statements 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12 will execute with normal termination.

Case 2: if an exception raised at statement-2 and the corresponding catch block matched then the statements 1, 10, 11, 12 will execute with normal termination.

Case 3: if an exception raised at statement-2 but the corresponding catch block not matched then the statements 1, 11, 12 will execute with abnormal termination.

Case 4: if an exception raised at statement-5 and the corresponding inner catch has matched then the statements 1, 2, 3, 4, 7, 8, 9, 11, 12 will execute with normal termination.

Case 5: if an exception raised at statement-5 and the inner catch has not matched but outer catch has matched then the statements 1, 2, 3, 4, 8, 10, 11, 12 will execute with normal termination.

Case 6: if an exception raised at statement-5 but the inner and outer catch blocks are not matched then the statements 1, 2, 3, 4, 8, 11 will execute with abnormal termination.

Case 7: if an exception raised at statement-7 and
1. If outer catch block has matched then the statements 1, 2, 3, - - - 8, 10, 11, 12 will execute with normal termination.
2. If the outer catch block has not matched then the statements 1, 2, 3, - - - 8, 11 will execute with abnormal termination.

Case 8: if an exception raised at statement-8 and
1. If outer catch has matched then the statements 1, 2, 3, - - - will execute with normal termination.
2. If outer catch has not matched then the statements 1, 2, 3, - - - 11 will execute with abnormal termination.

Case 9: if an exception raised at statement-9 and
1. If the outer catch has matched then the statements 1, 2, 3 - - -8, 10, 11, 12 will execute with normal termination.
2. If the outer catch has not matched then the statements 1, 2, 3 - - -8, 11 will execute with abnormal termination.

Case 10: if an exception raised at statement-10 it is always abnormal termination but before termination compulsory the finally block should be executed.

Case 11: if an exception raised at statement-11 or 12 it is always abnormal termination.

throw keyword
     By using throw we can hand – over exception object to the JVM. The out put of the following two programs is same.
E.g
class Test {
   public static void main(String arg[]) {
      System.out.println(10/0);
   }
}

class Test {
   public static void main(String arg[]) {
      throw new ArithmeticException ("/ by zero...!");
   } 
}
     Here in first case main method is responsible for the creation of exception object and hand – over that object to the JVM.

   In the second case we created object explicitly and hand – over that object to the JVM programmatically by throw key – word.

Syntax
throw e;
Where ‘e’ --> Any throwable object
E.g
class Test {
   public static void main(String arg[]) {
      throw new Test();
   }
}
class Test extends RuntimeException {
   public static void main(String arg[]) {
      throw new Test();
   }
}
class Test {
   static ArithmeticException e = new ArithmeticException();
   public static void main(String arg[]) {
      throw e;
   }
}
     Here Explicitly we created a object to the ArithmeticExcption class and that object was thrown by throw to the JVM.
class Test {
   static ArithmeticException e;
   public static void main(String arg[]) {
      throw e;
   }
}
   Here we didn’t create Object to the AritmeticExcepiton class just we created a reference, so reference variable is not pointing to any object and we thrown only reference variable that’s why only it shows NullPointerException.

    After throw keyword we are not allowed to place any statements directly other wise compile time error.
class Test {
   public static void main(String arg[]) {
      throw new ArithmeticException();
      System.out.println("After throw statement...!");
  }
}
   
     Directly in the sense indirectly we can place any statements after throw. See the following example.
class Test {
   public static void main(String arg[]) {
      if(false) {
         throw new ArithmeticException();
      } else {
         System.out.println("After throw statement...!");
      }
   }
}

throws
     If our code may be a chance of raising checked exception then compulsory we should handle that checked exception either by using try, catch or we have to delegate that responsibility to the caller using throws keyword other wise C.E saying
UnreportedException : XXXException must be caught or declared to be thrown
class Test {
   public static void main(String arg[]) {
      Thread.sleep(1000);
   }
}

     We can resolve this problem either by using try catch or by using throws keyword as follows
class Test {
   public static void main(String arg[]) {
      try {
         Thread.sleep(1000);
      }
      catch (InterruptedException e) {
      }
   }
}

class Test {
   public static void main(String arg[])throws InterruptedException {
      Thread.sleep(1000);
   }
}
     Hence the main objective of throws keyword is to delegate the responsibilities of exception handling to the caller.
class Test {
   public static void main(String arg[]) throws InterruptedException {
      doStuff();
   }
   public static void doStuff() throws InterruptedException {
      doMoreStuff();
   }
   public static void doMoreStuff() throws InterruptedException {
      Thread.sleep(500);
      System.out.println("I am in office");
   }
}
       If we are not taking at least one throws keyword we will get Compiler Error.
    If the parent class constructor throws some checked exception then the child class constructor must throw same checked exception or its parent other wise compiler error.

Summarization of Exception Handling Keywords
try        : To maintain risky code.
Catch   : To maintain exception handling code.
finally  : To maintain cleanup code.
throw   : To hand – over exception object to the JVM programmatically.
throws  : To delegate the responsibilities of exception handling to the caller.

Summarization of various compile time errors in Exception Handling
1. Exception has already been caught.
2. Exception never thrown in the body of corresponding try statement.
3. try with out catch or finally.
4. catch with out try.
5. finally with out try.
6. unreachable statement.
7. Incompatible types found: types

No comments:

Post a Comment