java 对象的抛出throw

class Demo{
    
    public int methond(int[] arr,int index)
    {
        
        if(arr==null){
            throw new NullPointerException("数组的引用不能为空");
    
        
        }
        if(index >=arr.length){
            throw new ArrayIndexOutOfBoundsException("数组的角标越界");
        }
        if(index <0){
            throw new ArrayIndexOutOfBoundsException("数组的角标不能为负数"); 
            
        }
        return arr[index];
    }
}



class TestDemo {
    public static void main(String[] args){
        System.out.println("hello libai");
        int[] arr = new int[3];
        Demo d= new Demo();
        int x  = d.methond(arr,3);
        System.out.println("this Array is "+x);
    }
    
    
}

标签: none