捕获异常时无限循环
Infinite loop while catching an exception
我正在尝试从 System.in 中获取一个整数,并且我想捕获最终的错误。这是代码:
int dim;
boolean done = false;
while(!done)
{
try
{
dim = in.nextInt();
done=true;
}
catch(InputMismatchException e)
{
System.out.println("I need an integer");
}
}
我得到的结果是错误字符串的无限循环。我做错了什么?我是否试图捕获错误的异常?
"When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method."Oracle
我正在尝试从 System.in 中获取一个整数,并且我想捕获最终的错误。这是代码:
int dim;
boolean done = false;
while(!done)
{
try
{
dim = in.nextInt();
done=true;
}
catch(InputMismatchException e)
{
System.out.println("I need an integer");
}
}
我得到的结果是错误字符串的无限循环。我做错了什么?我是否试图捕获错误的异常?
"When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method."Oracle