package as;
import java.util.Scanner;
public class d1 {
void inputException() {//定义inputException
int a[]=new int[3];//实例化,定义包含3个整数型元素的数组
Scanner sc=new Scanner(System.in);//从键盘上输入3个元素的值,并显示
System.out.println("请输入3个整型数据");
int i;
for(i=0;i<3;i++)
a[i]=sc.nextInt();
System.out.println("a["+i+"]="+a[i]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
d1 test=new d1();//创建属于fc的对象test
try {//为方法Exception()进行监视
test.inputException();
}
catch(ArrayIndexOutOfBoundsException e) {//捕获异常,并输入错误提示
System.out.println("数组越界"+e);
}
finally {//最终处理,输出相应信息提示
System.out.println("最后一定会被执行");
}
}
}
运行结果:
知识要点:
。异常的概念
。异常的分类
。声明异常
。抛出异常
。捕获和处理异常