问答题
【说明】本程序实现功能:读入两个整数,第1个数除以第2个数,声明当除数为零时抛出异常类DivideByZeroException。
public class DivideByZeroException (1) {
public DivideByZeroException ( ) {
super("Attcmpted to divide by zero");
}
}
import java.io. *;
public class Example {
private static int quotient(int numerator, in)\”}t denominator) throws
DivideByZeroException {
if (denominator==0)
throw (2) ;
return(numerator / denominator);
}
public static void main(String args[]) {
int number1=0, number2=0, result0;
try{
System.out.print1n("Enter the first number:");
number1 = Integer. valueOf(Keyboard.getString()).intValue();
System.out.print1n("Enter the second number:");
number2 = Integer. Va1ueOf(Keyboard.getString()).intValue();
result = quotient(number1,number2);
}
catch (NumberFormatException e) {
System.out.print1n("Invalid integer entered!");
System. exit(-1);
}
catch ( (3) ) {
System.out.print1n(e.to String());
System.exit(-1);
}
Systcm.out.pfint1n(number1 + "/" + number2 + "=" + result);
}
}
其中, Keyboard类的声明为:
import java.io.*;
public class Keyboard{
static BufferedReader inputStream = (4)
(new InputStreamReader(System.in));
public static int getInteger() {
try(
return (Intoger.valueOf(inputStream.readLine().trim()).intValue());
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static String getString() {
try{
return (inputStream.readLine());
} catch ( (5) )
{ return "0";}
}
}
【参考答案】
[解析] (1)extends ArithmeticException DivideByZeroException类从A......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)