题目详情:
常用异常类NumberFormatException,IndexOutOfBoundsException、ArithmeticException等,本程序,建立一个Test_Exception类,里面有一个test方法,用于检测以上异常对象的发生并输出相应信息。 比如发生NumberFormatException异常,输出"数据格式异常";发生IndexOutOfBoundsException异常,输出"越界异常";发生ArithmeticException异常,输出"算术运算异常"。 ##。# Test_Exception类定义:
class Test_Exception{
void test() {
int n = 0,m = 0,t = 1000;
int[] a=new int[4];
int[] b=null;
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
try {
m = Integer.parseInt(str);
t=t/m;
a[m]=m;
b[0]=m;
}
//catch部分需要你们自己填写
catch(...){...}
catch(...){...}
catch(...){...}
catch(Exception e) {
System.out.println("Exception:"+e.getMessage());
} //先catch子类Exception,后catch父类
}
}
前面3个catch 需要你 编写,发生对应异常,输出相应信息。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Test_Exception te=new Test_Exception();
te.test();
}
}
/* 请在这里编写完整 Test_Exception类 */
输入样例:
0
结尾无空行
输出样例:
算术运算异常
答案代码:
class Test_Exception
{
void test()
{
int n = 0,m = 0,t = 1000;
int[] a=new int[4];
int[] b=null;
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
try
{
m = Integer.parseInt(str);
t=t/m;
a[m]=m;
b[0]=m;
}
//catch部分需要你们自己填写
catch(NumberFormatException e){
System.out.println("数据格式异常");
}
catch(IndexOutOfBoundsException e){
System.out.println("越界异常");
}
catch(ArithmeticException e){
System.out.println("算术运算异常");
}
catch(Exception e) {
System.out.println("Exception:"+e.getMessage());
} //先catch子类Exception,后catch父类
}
}