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中报错之后的代码

posted @ 2025-02-07 00:45  Ryan_jxy  阅读(17)  评论(0)    收藏  举报