学Java的第30天 异常

package cn.aaa.exception;
/*异常中的关键字
 * throw 在方法内部  抛出异常对象
 * throw后面必须写   new  对象     必须是异常的对象:Exception或者Exception的子类
 * 方法当中有异常  调用者必须处理
 * */
public class ExceptionDemo1 {
    public static void main(String[] args) {
        int[] arr={};
        getArray(arr);
    }
   //方法  返回下标为6的 值
    public static int getArray(int[] arr)  {
        int i=arr[5];
        if(arr.length==0){
            try {
                throw new Exception("异常");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return 1;
        
    }

}

两种解决办法  抛出throws  或 编写 try/catch...

猜你喜欢

转载自www.cnblogs.com/--issing/p/10580977.html