import java.util.Scanner;
public class ExceptionTest1 {
void inputException()
{
int a[]=new int[3];
Scanner sc=new Scanner(System.in);
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
ExceptionTest1 test=new ExceptionTest1();
try
{
test.inputException();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("数组越界"+e);
}
finally
{
System.out.println("最后一定会被执行的语句");
}
}
}
运行结果:
知识要点:
异常的概念;
异常的分类;
声明异常;
创建类ExceptionTest1;
定义方法inputException();
从键盘输入3个整型元素的数组;