首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InputMismatchException错误

InputMismatchException错误
EN

Stack Overflow用户
提问于 2012-06-30 07:11:26
回答 1查看 962关注 0票数 0

我收到一个编译时错误,说:

不能引发InputMismatchException类型的异常;异常类型必须是Throwable InputMismatchException.java的子类

据我所知,InputMismatchException是扫描器在接收到无效输入时抛出的异常,那么为什么这个错误会阻止我进行编译?

代码语言:javascript
复制
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);
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-30 07:19:34

尝试为您的类使用不同的名称。如果有一个名为InputMismatchException的类,而这个类已经是一个异常类的名称,那么您就会混淆编译器。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11270048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档