【问题标题】:Try-Catch Exception Handling does not provide the correct responseTry-Catch 异常处理不提供正确的响应
【发布时间】:2018-11-14 01:07:10
【问题描述】:

我不确定为什么当我输入除整数值以外的任何内容时,输出未显示Invalid Format!

这就是我想要实现的异常处理。另外,我怎样才能在finally 子句中关闭Scanner 而不会导致无限循环。

class ConsoleInput {

public static int getValidatedInteger(int i, int j) {
    Scanner scr = new Scanner(System.in);
    int numInt = 0;

    while (numInt < i || numInt > j) {
        try {
            System.out.print("Please input an integer between 4 and 19 inclusive: ");
            numInt = scr.nextInt();

            if (numInt < i || numInt > j) {
                throw new Exception("Invalid Range!");
            } else {
                throw new InputMismatchException("Invalid Format!");
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            scr.next();

        }

    }
    scr.close();
    return numInt;
}

这是我想要得到的输出:

【问题讨论】:

    标签: java exception error-handling exception-handling try-catch


    【解决方案1】:

    如果您输入除 int 以外的任何内容,则会在该行抛出错误:

     numInt = scr.nextInt();
    

    然后在 catch 块中被捕获,从而跳过了 print 语句。您需要检查是否有下一个 int:

    if(!scr.hasNextInt()) {
       throw new InputMismatchException("Invalid Format!");
    }
    

    另外,如何在 finally 子句中关闭 Scanner 而不会导致无限循环。

    您不需要在 finally 块中关闭 Scanner。事实上,你根本不应该关闭它。关闭System.in 是不好的做法。通常,如果您没有打开资源,则不应关闭它。

    【讨论】:

      【解决方案2】:

      您需要在异常捕获块之前的单独捕获块中捕获 InputMismatchException 并添加 scr.next();如下:

      public static int getValidatedInteger(int i, int j) {
          Scanner scr = new Scanner(System.in);
          int numInt = 0;
      
          while (numInt < i || numInt > j) {
              try {
                  System.out.print("Please input an integer between 4 and 19 inclusive: ");
                  numInt = scr.nextInt();
      
                  if (numInt < i || numInt > j) {
                      throw new Exception("Invalid Range!");
                  }
              } catch (InputMismatchException ex) {
                  System.out.println("Invalid Format!");
                  scr.next();
              } catch (Exception ex) {
                  System.out.println(ex.getMessage());
                  scr.next();
      
              }
      
          }
          scr.close();
          return numInt;
      }
      

      【讨论】:

        【解决方案3】:

        以下代码将为您工作,这是您可以用来获得所需输出的另一种方法。 在这段代码中,我处理了 catch 块中的 InputMismatch。

        public static int getValidatedInteger(int i, int j) {
                    Scanner scr = new Scanner(System.in);
                    int numInt = 0;
        
                    while (numInt < i || numInt > j) {
                        try {
                            System.out.print("Please input an integer between 4 and 19 inclusive: ");
                            numInt = scr.nextInt();
        
                            if (numInt < i || numInt > j) {
                                System.out.println("Incorrect Range!");
                            }
        
                        } catch (InputMismatchException ex) {
                            System.out.println("Incorrect format!");
                            scr.next();
        
                        }
        
                    }
                    scr.close();
                    return numInt;
                }
        

        【讨论】:

          猜你喜欢
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-13
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 2011-01-01
          • 1970-01-01
          相关资源
          最近更新 更多