Thursday, January 30, 2014

Java Exception Handling: Basic Try Catch Finally

Source:

public class TryCatchFinally {
   public static void main(String[] args) {
 
      for (int count = 1; count != -1 ; count--) { 
 
         try {
           System.out.println("Inside try - count: "  + count);
           int x = 1 / count;
         } catch (Exception e){
           System.out.println("Inside catch - count: "  + count );
         } finally {
           System.out.println("Inside finally - count: "  + count );
         }
 
      }
 
   }
}


Output:
   $java TryCatchFinally 
   Inside try - count: 1
   Inside finally - count: 1
   Inside try - count: 0
   Inside catch - count: 0
   Inside finally - count: 0

Blog Archive