问题
I am receiving a compile time error saying:
No exception of type InputMismatchException can be thrown; an exception type must be a subclass of Throwable InputMismatchException.java
As far as I'm aware InputMismatchException is an exception thrown by the Scanner when it receives invalid input, why then is this error preventing me from compiling?
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);
}
}
回答1:
Try using a different name for your class. You're confusing the compiler by having a class named InputMismatchException
when that is already the name of an exception class.
来源:https://stackoverflow.com/questions/11270048/inputmismatchexception-error