import java.util.Scanner;
public class yichangshujuchuli { //创建类
void inputException() //定义方法
{
int a[]=new int[3];
System.out.println("请输入3个整型数据:");
Scanner sc=new Scanner(System.in);
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) throws IllegalAccessException{
// TODO 自动生成的方法存根
yichangshujuchuli test=new yichangshujuchuli(); //创建类yichangshujuchuli的对象
try{ //对inputException进行监视
test.inputException();
}
catch(ArrayIndexOutOfBoundsException e) //捕获异常,并显示错误
{
System.out.println("数组越界 "+e);
}
finally //输出相应提示
{
System.out.println("最后一定会被执行的语句");
}
}
}
运行结果如下: