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