我是Java编程的新手,我正在尝试创建一个扩展异常的自定义异常类。我给出的UML类图是:
UnluckyNumberException
unluckyNumber:int
UnluckyNumberException(int)的 getUnluckyNumber():INT
1)类需要扩展类异常,我假设是: public class UnluckyNumberException扩展Exception
2)它的构造函数应该接受异常所代表的“不幸数字”作为参数。该值应作为实例变量存储在对象中(作为int) *需要帮助*
3)你需要实现getUnluckyNumber方法,它在main中被调用。这仅返回存储在对象中的“不幸数字” *需要帮助*
这是主要的电话:
catch(UnluckyNumberException e) { System.out.printf(“错误:你输入了一个不吉利的数字:%d \ n”,e.getUnluckyNumber()); }
答案 0 :(得分:0)
EXCETION:
public class UnluckyNumberException extends RuntimeException {
private final int unluckyNumber;
public UnluckyNumberException(int unluckyNumber) {
this.unluckyNumber = unluckyNumber;
}
public int getUnluckyNumber() {
return unluckyNumber;
}
}
主:
public static void main(String[] args) {
try {
...
throw new UnluckyNumberException(3);
...
} catch (UnluckyNumberException e) {
System.out.printf ("Error: you entered an unlucky number: %d\n", e.getUnluckyNumber());
}
}