这是一个代码示例。如果我要输入分子:5 和分母:0
我得到一个这样的例外:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionHandling.DivideByZeroExceptions.quotient(DivideByZeroExceptions.java:10)
at ExceptionHandling.DivideByZeroExceptions.main(DivideByZeroExceptions.java:22)
我知道我必须包含(抛出算术异常)但是,我怎么知道我需要使用 inputMismatchException?
// Try DivideByZeroExceptions
public class DivideByZeroExceptions {
public static int quotient(int numerator, int denominator) {
return numerator / denominator;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an integer numerator: ");
int numerator = input.nextInt();
System.out.println("Please enter an integer denominator: ");
int denominator = input.nextInt();
int result = quotient(numerator, denominator);
System.out.printf("\nResult: %d / %d = %d\n", numerator, denominator,
result);
}
}