给代码做异常处理

public class seven2{
    public static void main(String[] args){
        int score[];
        score=new int[3];
        try{                                //会出现异常的代码块用try括起来
            for(int x=0;x<4;x++)            //定义的数组长度为三,循环却跑到了四,属于数组越界
        score[x]=x*2+1;
            for(int x=0;x<3;x++)
        System.out.println("score["+x+"]="+score[x]);
        }
        catch(ArrayIndexOutOfBoundsException e){        //抛出数组越界异常
            System.out.println("数组越界异常:"+e);        
        }
    }
}

 

posted @ 2018-09-16 17:40  乐丶Sir  阅读(185)  评论(0编辑  收藏  举报