我收到一个编译时错误说:
不能抛出 InputMismatchException 类型的异常;异常类型必须是 Throwable InputMismatchException.java 的子类
据我所知 InputMismatchException 是 Scanner 在收到无效输入时抛出的异常,为什么这个错误会阻止我编译?
import java.util.*;
public class InputMismatchException
{
public static void main(String[] args)
{
boolean continueInput = true;
Scanner input = new Scanner(System.in);
do
{
try
{
System.out.println("Enter an integer: ");
int num = input.nextInt();
System.out.println("You entered: " + num);
continueInput = false;
}
catch (InputMismatchException e) //This is where the error occurs.
{
System.out.println("Enter an integer!");
input.nextLine();
}
}while(continueInput);
}
}