javacodex.com
Java Examples
Java Examples
memu home questions

Handling Multiple Catch Clauses

The following java example demonstrates handling multiple catch clauses.

Source: (ExceptionMatching.java)

public class ExceptionMatching {
   public static void main(String[] args) {
 
      String[] str = new String[2]; 
      str[0] = "String 0";
 
      for (int x = 0; x < 3; x++) {
         try {
            System.out.println("String length: " + str[x].length());
         } catch (NullPointerException e) {
            System.out.println("Handling Null Pointer Exception");
         } catch ( ArrayIndexOutOfBoundsException e) {
            System.out.println("Handling ArrayIndexOutOfBounds Exception");
         } catch (Exception e) {
            System.out.println("Handling Unknown Exception");
         }
      }
   }
}
 

Output:

# java ExceptionMatching 
String length: 8
Handling Null Pointer Exception
Handling ArrayIndexOutOfBounds Exception

Contact: javacodex@yahoo.com