6 (Optional). Define a class TestDivisionException, the requirements are as follows (1) Define the static method divisionNum, receive two input integers, return the divisible result, and use throws to throw ArithmeticExc

6 (Optional). Define a class TestDivisionException, the requirements are as follows
(1) Define the static method divisionNum, receive two input integers, return the divisible result, and use throws to throw an ArithmeticException exception; (
2) The main method receives keyboard input two Integers a, b; call the divisionNum method, and store the result in the variable c,
(3) If the divisor is 0, catch the exception;
(4) Print out the error message and error stack information;
(5) Finally, whether there is an error or not Print out the result of c, and print "computed".

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计算完毕。");
    }
}

Guess you like

Origin blog.csdn.net/winds_tide/article/details/116949631