use try catch to handle exception in java
/**
* @author lautturi.com
* Java example: handle exception example in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3, 4, 5, 6};
System.out.println(myNumbers[7]);
} catch (Exception e) {
System.out.println("exception occurs");
System.out.println(e);
}
}
}
output:
exception occurs java.lang.ArrayIndexOutOfBoundsException: 7