我试图覆盖 Java 中 NumberFormatException 类中的 getMessage() 方法,这是一个未经检查的异常。出于某种原因,我无法覆盖它。我知道这一定很简单,但不明白我可能会错过什么。有人可以帮忙吗?这是我的代码:
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();
}
}
}