java Catch 不是捕获 InputMismatchException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25719588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catch isn't catching InputMismatchException?
提问by Will Troll
So, I've searched around google and stackoverflow for a bit, but I can't seem to find an answer to this issue.
所以,我在 google 和 stackoverflow 上搜索了一些,但我似乎无法找到这个问题的答案。
I've got 2 methods. The first method, getAge is meant to just get an integer as input from the user. It's then meant to pass that input to verifyAge, who makes sure it's in the right range.
我有2种方法。第一种方法 getAge 旨在从用户那里获取一个整数作为输入。然后打算将该输入传递给verifyAge,后者确保它在正确的范围内。
However; if they should enter anything that is not an integer, it's supposed to display a message and call getAge again, to restart the input process. I have a try-catch set up, but it still goes back to JVM. According to an answer in another post; what I'm doing is correct. But it still doesn't seem to be working. So here's the errors I get when I try to run it as I have it now:
然而; 如果他们应该输入任何不是整数的东西,它应该显示一条消息并再次调用 getAge 以重新启动输入过程。我有一个 try-catch 设置,但它仍然回到 JVM。根据另一篇文章的回答;我在做什么是正确的。但它似乎仍然不起作用。所以这是我尝试运行它时遇到的错误,因为我现在拥有它:
Please enter your age: notint
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Ch2ProgLabWilson.getAge(Ch2ProgLabWilson.java:22)
at Ch2ProgLabWilson.main(Ch2ProgLabWilson.java:15)
What I have written:
我写的:
import java.util.* ;
import java.util.Scanner;
public class Ch2ProgLabWilson {
public static void main(String[] args) {
getAge();
}
public static int getAge()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int a = keyboard.nextInt();
verifyAge(a);
try
{
getAge();
}
catch (InputMismatchException e)
{
System.out.println("You may only enter integers as an age. Try again.");
getAge();
}
return a;
}
//
public static boolean verifyAge (int a)
{
if (a >= 0 && a <= 122)
{
System.out.println("The age you entered, " + a + ", is valid.");
return true;
}
else
{
System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
getAge();
return false;
}
}
}
回答by Romski
The Exception is being thrown by int a = keyboard.nextInt();
, which is outside of the try catch block.
Place the call to int a = keyboard.nextInt();
inside the try block.
异常由 抛出int a = keyboard.nextInt();
,它在 try catch 块之外。将调用int a = keyboard.nextInt();
放在 try 块内。
There are other problems with your code:
您的代码还有其他问题:
verifyAge()
returns a boolean
that is never used.
verifyAge()
返回一个boolean
从未使用过的。
Your getAge()
method is recursive and, assuming that the user enters a number, it will just loop - is this what you intended?
你的getAge()
方法是递归的,假设用户输入一个数字,它只会循环 - 这是你想要的吗?
UPDATE
更新
public static int getAge(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int age = -1;
while(!verifyAge(age)){ // will loop until there's a valid age
try{
age = scanner.nextInt();
catch (InputMismatchException e){
System.out.println("You may only enter integers as an age. Try again.");
}
}
return age; // your current code doesn't do anything with this return value
}
public static boolean verifyAge (int a){ // would be better named isValidAge()
if (a >= 0 && a <= 122){
System.out.println("The age you entered, " + a + ", is valid.");
return true;
}else{ // no need to call getAge() here
System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
return false;
}
}
回答by Scary Wombat
well the code is not within the try
block
那么代码不在try
块内
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int a = keyboard.nextInt();
verifyAge(a);
try
{
so it will not get caught.
所以它不会被抓住。
回答by PKlumpp
Look at the line of code which actually throwsthe Exception. If it is not in the try/catch
block, it will not be caught.
查看实际抛出异常的代码行。如果它不在try/catch
块中,则不会被捕获。
Try this instead:
试试这个:
try
{
int a = keyboard.nextInt();
verifyAge(a);
}
catch (InputMismatchException e)
{
System.out.println("You may only enter integers as an age. Try again.");
getAge();
}