异常捕捉
try{}
catch{}
import java.util.Scanner;public class ArrayIndex {public static void main(String[] args) {int [] a =new int[10];int idx;Scanner in = new Scanner(System.in);idx = in.nextInt();try{a[idx] = 10;System.out.println("hello");}catch(ArrayIndexOutOfBoundsException e){System.out.println("数组越界");}}
}
若在try中报错,直接跳到catch中,不会运行try中报错之后的代码