25.2.7小记
异常捕捉
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中报错之后的代码







浙公网安备 33010602011771号