我是靠谱客的博主 拼搏小鸽子,这篇文章主要介绍非运行时异常处理,现在分享给大家,希望可以做个参考。

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

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

}

最后

以上就是拼搏小鸽子最近收集整理的关于非运行时异常处理的全部内容,更多相关非运行时异常处理内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(147)

评论列表共有 0 条评论

立即
投稿
返回
顶部