我收到一个编译时错误,说:
不能引发InputMismatchException类型的异常;异常类型必须是Throwable InputMismatchException.java的子类
据我所知,InputMismatchException是扫描器在接收到无效输入时抛出的异常,那么为什么这个错误会阻止我进行编译?
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);
}
}发布于 2012-06-30 07:19:34
尝试为您的类使用不同的名称。如果有一个名为InputMismatchException的类,而这个类已经是一个异常类的名称,那么您就会混淆编译器。
https://stackoverflow.com/questions/11270048
复制相似问题