实验题1
在程序中产生一个异常运算类型的异常ArithmeticException(如:一个整数“除以零”时),并用catch 语句捕获这个异常。最后通过ArithmeticException 类的对象e 的方法getMessage 给出异常的具体类型并显示出来。
import java.util.*;
class Divide{
double a;
double b;
Divide(double a, double b){
this.a = a;
this.b = b;
}
public void jiSuan() {
double c = a / b;
if (b == 0) throw new ArithmeticException("除数不能为零!!!");
System.out.println("" + a + " / " + b + " = " + c);
}
}
public class A {
public static void main(String[] args) {
try {
Scanner rd = new Scanner(System.in);
double a = rd.nextDouble();
double b = rd.nextDouble();
rd.close();
Divide d = new Divide(a, b);
d.jiSuan();
} catch (ArithmeticException e) {
System.out.println("显示的信息: " + e.getMessage());
}
}
}