1. 程式人生 > >給代碼做異常處理

給代碼做異常處理

bound 代碼塊 void [] mage exce static string try

技術分享圖片

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

技術分享圖片

給代碼做異常處理