6(选做).定义一个类TestDivisionException,要求如下
(1)定义静态方法divisionNum,接收输入的两个整数数,返回整除结果,使用throws抛出ArithmeticException异常;
(2)main方法里接收键盘输入两个整数a,b;调用divisionNum方法,结果存入变量c,
(3)如果除数为0,对异常进行捕获;
(4)打印输出错误信息和错误堆栈信息;
(5)最后不管是否有错误都打印输出c的结果,并打印“计算完毕”。
package cn.edu.ahtcm.bean;
import java.util.Scanner;
public class TestDivisionException {
public static int divisionNum(int number1,int number2){
if(number2==0)
throw new ArithmeticException("除数不能为零!");
return number1/number2;
}
public static void main(String[] args) {
Scanner a =new Scanner(System.in);
System.out.println("请输入数字:");
int number1=a.nextInt();
int number2=a.nextInt();
double c=0;
try{
c=divisionNum(number1,number2);
}
catch (ArithmeticException ex){
System.out.println("除数不能为零!");
ex.printStackTrace();
}
System.out.println("c="+c+"\n计算完毕。");
}
}