Thursday, January 30, 2014

The use of try and catch in java.

Source:
public class BasicTryCatch {
   public static void main(String[] args) {
 
      int[] array = new int[3];
 
      try {
         array[4] = 1; // outside its boundry
      } catch (Exception e) {
        System.out.println("Exception thrown: " + e.toString());
      }
 
   }
}

Output:
   $ java BasicTryCatch 
   Exception thrown: java.lang.ArrayIndexOutOfBoundsException: 4

Blog Archive