javacodex.com
Java Examples
Java Examples
memu home questions

Basic Try Catch

This is a basic exception handling program that uses try and catch blocks.

Source: (BasicTryCatch.java)

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

Contact: javacodex@yahoo.com