Try/Catch 异常未按计划运行

Try/Catch Exception not working as planned

我对此感到困惑 - 我为辅助方法写了一个 try/catch。它的目的是捕获任何无效输入(任何不是 "male" 或 "female"(无特定情况)的输入。如果输入无效,它将通知用户,然后让他们重试。如果它有效,该方法将 return 输入。

当我 运行 程序时,它不会捕获无效输入。为什么这不起作用?

这是辅助方法:

//Helper method that gathers the string input from user
public static String getString() {

    //Create new scanner input
    Scanner input = new Scanner(System.in);

    //Declare and initialize variables
    String wordIn = "";
    boolean validIn;

    //do/while loop for obtaining input and checking validity (try/catch)
    do {
        validIn = true;
        try{
            wordIn = input.nextLine();

        //End Try   
        }

        catch(Exception invalidInput) {
            validIn = false;
            input = new Scanner(System.in);
            System.out.print("\nYou have entered an invalid input. Please "
                    + "enter either \"Male\" or \"Female\" ONLY for this "
                    + "selection. The selection is not case sensitive."
                    + "\n\nPlease enter your selection: ");

        //End Catch    
        }

    //While input is valid, return the input    
    } while (!validIn);
     return wordIn;

//End helper
}

测试代码如下:

//Obtain user input and print output
String passGender = getString();
System.out.println("\n" + titanic.getSurvivedPassengersGender(passGender)
                        + " " + passGender.toLowerCase() + " passengers survived the "
                        + "sinking of the Titanic.");

好像我没有设置正确的条件...我找不到哪里出错了。我对此还是陌生的,所以我确定这是一个简单的错误。任何帮助是极大的赞赏。谢谢大家!

你没有为你想要的设置任何条件。 您没有设置任何条件来引发非 'male' 或 'female' 的输入事件。你的代码应该是:

//Helper method that gathers the string input from user
public static String getString() {

//Create new scanner input
Scanner input = new Scanner(System.in);

//Declare and initialize variables
String wordIn = "";
boolean validIn;

//do/while loop for obtaining input and checking validity (try/catch)
do {
    validIn = true;
    try{
        wordIn = input.nextLine();
        if(!(wordIn.equalsIgnoreCase("male") || wordIn.equalsIgnoreCase("female")))
            throw new Exception();
    //End Try   
    }

    catch(Exception invalidInput) {
        validIn = false;
        input = new Scanner(System.in);
        System.out.print("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");

    //End Catch    
    }

//While input is valid, return the input    
} while (!validIn);
 return wordIn;
//End helper
}

编辑 另外,就像@ashutosh 说的,你不必抛出异常,你可以只使用一个条件:

wordIn = input.nextLine();
if(!(wordIn.equalsIgnoreCase("male") || wordIn.equalsIgnoreCase("female"))
     System.out.print("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");

或者(正如评论中的许多人所说)您最好改用条件。类似于:

wordIn = input.next(); 

while(!(wordIn.toLowerCase().equals("male") || wordIn.toLowerCase().equals("female"))){

System.out.println("\nYou have entered an invalid input. Please "
                + "enter either \"Male\" or \"Female\" ONLY for this "
                + "selection. The selection is not case sensitive."
                + "\n\nPlease enter your selection: ");
wordIn = input.next();

}

这将一直循环直到输入有效的字符串,而不需要 try catch。