非运行时异常处理

/*
运行时异常:编译时不检测

非运行时异常:
当使用了 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);
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值