给代码做异常处理

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);        
        }
    }
}

给代码做异常处理

 

相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2021-06-20
  • 2021-07-06
  • 2021-09-20
  • 2021-06-11
  • 2021-04-05
  • 2021-11-13
猜你喜欢
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-11-06
  • 2021-10-16
  • 2021-07-14
相关资源
相似解决方案