Topic details:
Common exception classes are NumberFormatException, IndexOutOfBoundsException, ArithmeticException, etc. This program creates a Test_Exception class with a test method to detect the occurrence of the above exception objects and output corresponding information. For example, if a NumberFormatException occurs, "data format exception" is output; when an IndexOutOfBoundsException occurs, "out of bounds exception" is output; when an ArithmeticException occurs, "arithmetic operation exception" is output. ##. # Test_Exception class definition:
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父类
}
}
The first three catches need to be written by you, the corresponding exception occurs, and the corresponding information is output.
Example of the referee test procedure:
在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Test_Exception te=new Test_Exception();
te.test();
}
}
/* 请在这里编写完整 Test_Exception类 */
Input sample:
0
no blank line at the end
Sample output:
算术运算异常
Answer code:
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父类
}
}