我试图覆盖Java中的NumberFormatException类中的getMessage()方法,该方法是未经检查的异常。出于某种原因,我无法覆盖它。我知道这一定很简单,但不明白我可能会错过什么。有人可以帮忙吗?这里是我的代码:从Java中未经检查的Exception类中覆盖方法
public class NumberFormatSample extends Throwable{
private static void getNumbers(Scanner sc) {
System.out.println("Enter any two integers between 0-9 : ");
int a = sc.nextInt();
int b = sc.nextInt();
if(a < 0 || a > 9 || b < 0 || b > 9)
throw new NumberFormatException();
}
@Override
public String getMessage() {
return "One of the input numbers was not within the specified range!";
}
public static void main(String[] args) {
try {
getNumbers(new Scanner(System.in));
}
catch(NumberFormatException ex) {
ex.getMessage();
}
}
}
实际上规范提到我应该只抛出一个NumberFormatException对象。 – Setafire 2013-05-01 01:10:11
@Setafire:查看我的编辑。 – jlordo 2013-05-01 01:12:48
是的,我刚刚得到了。正在编辑原文,但您已经先编辑了您的文章。 – Setafire 2013-05-01 01:18:01