/*
运行时异常:编译时不检测
非运行时异常:
当使用了 throws声明,那么调用方必须处理,处理方式有两种 (必须对其进行捕获或声明以便抛出)
1:使用try{}catch(){}处理
2:使用throws声明抛出
throw:用于手动抛出异常类对象
用了throw,必须处理,处理方式有两种:
1:使用try{}catch(){}
2:使用throws声明
*/
//获取数组中指定下标的数据
class Tests
{
//获取数组中指定下标的数据
public int getNum(int[] arr,int index)throws Exception
{
if(index<0 || index>arr.length-1)
throw new ArrayIndexOutOfBoundsException();
if(arr==null)
throw new NullPointerException();
return arr[index];
}
}
class Demo10
{
public static void main(String[] args)
{
int[] arr={2,3,4,5,6};
Tests tests = new Tests();
int a = tests.getNum(null,3);
System.out.println(a);
}
}