Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不捕获多个异常_Java_Exception Handling - Fatal编程技术网

Java 不捕获多个异常

Java 不捕获多个异常,java,exception-handling,Java,Exception Handling,如果输入值介于1-26之间,则返回该值。当我输入一个大于27的数字或一个字母时,它不会捕获并处理异常,只会跳过该行继续循环 它为什么会这样做,我如何修复它 public int checkInput(int userInput) { boolean valid = false; do { try { if (userInput <= 26 || userInput > 0) { break;

如果输入值介于1-26之间,则返回该值。当我输入一个大于27的数字或一个字母时,它不会捕获并处理异常,只会跳过该行继续循环

它为什么会这样做,我如何修复它

 public int checkInput(int userInput) {

    boolean valid = false;

    do {
        try {
            if (userInput <= 26 || userInput > 0) {
                break;
            }
            valid = true;

        } catch (NumberFormatException | InputMismatchException |
                 ArrayIndexOutOfBoundsException e) {
            valid = false;
            System.out.println("The input is invalid, please enter the answer again.");
            userInput = sc.nextInt();
        }
    } while (!valid);

    return userInput;
}
public int checkInput(int userInput){
布尔有效=假;
做{
试一试{
如果(用户输入0){
打破
}
有效=真;
}catch(NumberFormatException | InputMismatchException|
阵列索引边界外(e){
有效=错误;
System.out.println(“输入无效,请重新输入答案”);
userInput=sc.nextInt();
}
}而(!有效);
返回用户输入;
}

提前感谢

正如托马斯·克勒格尔(Thomas Kläger)所写的那样,
catch
子句永远不会到达。
try
子句中的任何内容都不会引发异常:

if (userInput <= 26 || userInput > 0) {
    break;
}
valid = true;
异常发生在
int input=sc.nextInt()
行,但这不在
try
子句中


您正在使用异常作为控制流的一部分。 这被认为是不好的做法,参见

以下工作按预期进行

package main;

import java.util.Scanner;

public class ExceptionHandling {
    public static void main(String[] args) {
        final ExceptionHandling exceptionHandling = new ExceptionHandling();
        exceptionHandling.getInputUntilValid();
    }

    private final Scanner scanner;

    public ExceptionHandling() {
        this.scanner = new Scanner(System.in);
    }

    public void getInputUntilValid() {
        boolean isValidInput = false;
        while (!isValidInput) {
            if (scanner.hasNext()) {
                Optional<Integer> integerInput = getInt();
                isValidInput = checkInput(integerInput);
                if (!isValidInput) {
                    System.out.println("The input is invalid, please enter the answer again.");
                }
            }
        }
        System.out.println("Valid.");
    }

    private Optional<Integer> getInt() {
        if (scanner.hasNextInt()) {
            int input = scanner.nextInt();
            return Optional.of(input);
        }
        else {
            scanner.next();
            return Optional.empty();
        }
    }

    private boolean checkInput(Optional<Integer> input) {
        return input.map(value -> checkBounds(value)).orElse(false);
    }

    private boolean checkBounds(int userInput) {
        return 0 < userInput && userInput <= 26;
    }
}
packagemain;
导入java.util.Scanner;
公共类异常处理{
公共静态void main(字符串[]args){
最终异常处理异常处理=新异常处理();
exceptionHandling.getInputUntilValid();
}
私人最终扫描仪;
公共例外处理(){
this.scanner=新扫描仪(System.in);
}
public void getInputUntilValid(){
布尔值isValidInput=false;
而(!isValidInput){
if(scanner.hasNext()){
可选的integerInput=getInt();
isValidInput=检查输入(整数输入);
如果(!isValidInput){
System.out.println(“输入无效,请重新输入答案”);
}
}
}
System.out.println(“有效”);
}
私有可选getInt(){
if(scanner.hasNextInt()){
int input=scanner.nextInt();
返回可选。of(输入);
}
否则{
scanner.next();
返回可选的.empty();
}
}
专用布尔校验输入(可选输入){
返回input.map(value->checkBounds(value)).orElse(false);
}
私有布尔校验边界(int userInput){

返回0
import java.util.Scanner;

public class TestRun {

    static int first_value = 27;

    public static void main(String args[]) {

        int value = checkInput2(first_value);

        System.out.println(value);

    }

    public static int checkInput2(int userInput) {

        if (!checkvalue(userInput)) {

            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("The input is invalid, please enter the answer again.");
                userInput = sc.nextInt();
                checkInput2(userInput);

            } catch (Exception e) {
                checkInput2(first_value);
            }
        }
        return userInput;
    }

    public static boolean checkvalue(int userInput) {

        if (userInput <= 26 && userInput > 0) {
            return true;
        }
        return false;
    }
}
import java.util.Scanner;
公共类测试运行{
静态int first_值=27;
公共静态void main(字符串参数[]){
int值=checkInput2(第一个值);
系统输出打印项次(值);
}
公共静态int checkInput2(int userInput){
如果(!检查值(用户输入)){
试一试{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入无效,请重新输入答案”);
userInput=sc.nextInt();
checkInput2(用户输入);
}捕获(例外e){
checkInput2(第一个值);
}
}
返回用户输入;
}
公共静态布尔校验值(int userInput){
如果(用户输入0){
返回true;
}
返回false;
}
}

您的代码不会抛出任何异常。因此,将永远无法到达catch块。此代码甚至不会编译。您应该抛出一个用户定义的异常,而不是中断。如果您希望在输入有效值之前一直提示,则无需抛出异常
import java.util.Scanner;

public class TestRun {

    static int first_value = 27;

    public static void main(String args[]) {

        int value = checkInput2(first_value);

        System.out.println(value);

    }

    public static int checkInput2(int userInput) {

        if (!checkvalue(userInput)) {

            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("The input is invalid, please enter the answer again.");
                userInput = sc.nextInt();
                checkInput2(userInput);

            } catch (Exception e) {
                checkInput2(first_value);
            }
        }
        return userInput;
    }

    public static boolean checkvalue(int userInput) {

        if (userInput <= 26 && userInput > 0) {
            return true;
        }
        return false;
    }
}