import java.util.Scanner;
public class Ex { //创建类Ex
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) {
Ex text =new Ex(); //创建属于类Ex的对象text
try{ //为方法inputException()进行"监视"
text.inputException();
}
catch(ArrayIndexOutOfBoundsException e){ //捕获异常,并输入错误提示
System.out.println("数组越界"+e);
}
finally{ //最终处理,输出相应信息提示
System.out.println("最后一定会被执行的语句");
}
}
public class Ex { //创建类Ex
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) {
Ex text =new Ex(); //创建属于类Ex的对象text
try{ //为方法inputException()进行"监视"
text.inputException();
}
catch(ArrayIndexOutOfBoundsException e){ //捕获异常,并输入错误提示
System.out.println("数组越界"+e);
}
finally{ //最终处理,输出相应信息提示
System.out.println("最后一定会被执行的语句");
}
}
}
知识点:1、异常的概念和分类
2、声明异常
3、抛出异常
4、捕获和处理异常