【问题标题】:Not catching multiple exceptions没有捕获多个异常
【发布时间】:2018-03-27 10:37:31
【问题描述】:

如果输入在 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;
}

提前致谢

【问题讨论】:

  • 您的代码不会抛出任何异常。因此永远不会到达 catch 块
  • 这段代码甚至无法编译。
  • 你应该抛出一个用户定义的异常而不是中断
  • 如果您的意图是一直提示直到输入有效值,则无需抛出异常

标签: java exception-handling


【解决方案1】:

试试这个。我只是尝试使用递归概念。

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;
    }
}

【讨论】:

    【解决方案2】:

    正如 Thomas Kläger 所写,catch 子句永远不会到达。 try 子句中的任何内容都不会抛出异常:

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

    您没有提供所有代码,因为我们看不到checkInput(int) 是如何被调用的。 但我假设你这样称呼它:

    public void takeInput() {
        int input = sc.nextInt();
        checkInput(input);
    }
    

    异常发生在int input = sc.nextInt() 行,但这不在try 子句中。


    您正在使用异常作为控制流的一部分。 这被认为是不好的做法,请参阅 https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why

    以下按预期工作。

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 2021-06-20
      • 2013-03-24
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多