InputMismatchException Error

限于喜欢 提交于 2019-12-25 05:04:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!