第一题
【NumberFormatException异常】编写一个程序,提示用户输入两个整数,然后显示它们的和。用户输入错误时提示用户重新输入。
(1)运行结果
测试一:
测试二:
(2)结果分析
测试1输入了两个整数,直接输出和
测试2前两次的输入中都包含浮点数,第三次输入正确,并输出了和
(3)心得体会
此题是对NumberFormatException的处理,可用flag作为while语句的指标,提示用户直到输入正确为止,是异常处理的基础运用,难度不大
(4)源代码
package 实验6;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入两个整数");
boolean flag = false;
while(!flag){
try{
String a = input.next();
String b = input.next();
System.out.println("这两个整数的和为:"+(Integer.valueOf(a)+Integer.valueOf(b)));
flag = true;
}
catch(NumberFormatException ex){
System.out.println("您输入了不符合要求的数,请重新输入:");
}
}
}
}
第二题
【ArrayIndexOutOfBoundsException异常】编写一个程序,创建一个由100个随机选取的整数构成的数组;提示用户输入数组下标,然后显示元素的值,如果指定的下标越界,显示消息out of bounds。
(1)运行结果
测试1:
测试2:
测试3:
测试4:
(2)结果分析
测试1与测试4输入的都不是0-99的数,故输出Out of bounds
测试2与测试3输入正确,输入相应位置的随机数
(3)心得体会
数组越界是很常见的异常类型了,由于此题创建的是100个元素的数组,所以输入值为0-99之间的整数。
(4)源代码
package 实验6;
import java.util.Scanner;
public class Program2 {
public static void main(String[] args){
int[] list = new int[100];
for(int i=0;i<100;i++){
list[i] = (int) (Math.random()*1000);
}
Scanner input = new Scanner(System.in);
System.out.println("请输入一个0-99的数");
int i = input.nextInt();
try{
System.out.println("第"+i+"个随机数为:"+list[i]);
}
catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Out of bounds");
}
}
}
第三题
请将393页图12-3所示的内容转换成可运行的代码,尝试抛出不同类型的异常,理解寻找异常处理器的过程及代码执行的流程。
(1)运行结果
测试1:
测试2:
测试3:
(2)结果分析
测试一:调用三个方法前的语句都会调用,method3执行完后抛出空指针异常,直到返回到主函数里才被捕捉,之间的语句全都不执行
测试二:与测试一不同的是下标越界异常在method1里就被捕捉,故相较于测试一会执行method1的try catch块后的语句和主函数调用method1后的语句。
测试三:与测试二不同的是运算条件异常在method2里就被捕捉,故相较于测试二会执行method2的try catch块后的语句和method1调用method2后的语句。
(3)心得体会
哪些语句会调用,哪些不会,做个实验就行了。这个实验可以帮我们巩固这方面知识。
(4)源代码
package 实验6;
import java.util.Scanner;
public class Program3 {
public static void method1(int flag){
try{
System.out.println("method1调用method2前语句已被调用");
method2(flag);
System.out.println("method1调用method2后语句已被调用");
}
catch(IndexOutOfBoundsException ex){
System.out.println("method1捕捉到下标越界异常");
}
System.out.println("method1的try catch块后语句已被调用");
}
public static void method2(int flag){
try{
System.out.println("method2调用method3前语句已被调用");
method3(flag);
System.out.println("method2调用method3后语句已被调用");
}
catch(ArithmeticException ex){
System.out.println("method2捕捉到运算条件异常");
}
System.out.println("method2的try catch块后语句已被调用");
}
public static void method3(int flag){
if(flag==1) throw new NullPointerException();
if(flag==2) throw new IndexOutOfBoundsException();
if(flag==3) throw new ArithmeticException();
}
public static void main(String[] args) {
System.out.println("请输入一个1,2,3中的一个数,输入一代表method3抛出空指针异常,");
System.out.println("输入2代表method3抛出下标越界异常,输入3代表method3抛出运算条件异常");
Scanner input = new Scanner(System.in);
int flag = input.nextInt();
try{
System.out.println("主函数调用method1前语句已被调用");
method1(flag);
System.out.println("主函数调用method1后语句已被调用");
}
catch(NullPointerException ex){
System.out.println("主函数捕捉到空指针异常");
}
System.out.println("主函数的try catch块后语句已被调用");
}
}