【问题标题】:Understanding try & catch and error handling了解 try & catch 和错误处理
【发布时间】:2014-09-19 18:31:18
【问题描述】:
import java.util.Scanner;

public class Lab4_5 {
    public static void main(String[]args) {
        Scanner scan= new Scanner(System.in);

        int rows=0;
        int rowIndex=0, colIndex=0;
        boolean choice1= true;
        String y="y";
        String n="n";
        boolean first = true;

        while (choice1==true) {
            if (first==true) {  
                first=false;
                System.out.println("Do you want to start(Y/N): ");
            } else if (first==false) {
                System.out.println("Do you want to continue(Y/N): ");
            }

            String choice2=scan.next();
            if (choice2.equals(y)) {
                System.out.println("How many rows/columns(5-21)?");
                rows=scan.nextInt();
                while (rows<5 || rows>21) {
                    System.out.println("That is either out of range or not an integer, try again! ");
                    rows=scan.nextInt();
                }

                System.out.println("What character?");
                String choice3=scan.next();
                System.out.println(" ");

                for (rowIndex=1; rowIndex<=rows; rowIndex++) {
                    for (colIndex=1; colIndex<=rows; colIndex++) {
                        if (rowIndex==1 || rowIndex==rows || colIndex==1 || colIndex==rows) {
                            System.out.print(choice3);
                        } else {
                            System.out.print(" ");
                        }
                    }
                    System.out.println();
                }
            } else if(choice2.equals(n)) {
                choice1 = false;
                System.out.println("Thank you. Goodbye.");
            } else {
                System.out.println("Please either enter Y or N.");
            }
        }
    }//end of main 
}

代码打印我需要打印的内容,但是当它询问有多少行/列来捕获我是否输入了除整数以外的其他内容时,我还必须在代码中包含一些内容(在下面的部分中)。需要一些帮助,我们还没有做任何关于如何捕获异常的事情,我不知道如何开始。

String choice2=scan.next();
if (choice2.equals(y)) {
    System.out.println("How many rows/columns(5-21)?");
    rows=scan.nextInt();
    while (rows<5 || rows>21) {
        System.out.println("That is either out of range or not an integer, try again! ");
        rows=scan.nextInt();
    }
}

【问题讨论】:

  • 您是遇到异常还是只想知道如何让代码运行起来更具可读性?

标签: java error-handling try-catch


【解决方案1】:

您需要了解this请查看。

基本理解是

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught.
} 

还有一个 finally 块,即使出现错误也将始终执行。它是这样使用的

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught & the returned.
} finally {
  // This will always execute if there is an exception or no exception.
}

在您的特定情况下,您可以有以下例外 (link)。

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围 NoSuchElementException - 如果输入已用尽 IllegalStateException - 如果此扫描仪已关闭

所以你需要捕捉像

这样的异常
try { 
   rows=scan.nextInt();
} catch (InputMismatchException e) {
  // When the InputMismatchException is caught.
  System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
  // When the NoSuchElementException is caught.
  System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
  // When the IllegalStateException is caught.
  System.out.println("Scanner is close");
} 

【讨论】:

    【解决方案2】:

    你可以像这样创建一个 try-catch 块:

    try {
        int num = scan.nextInt();
    } catch (InputMismatchException ex) {
        // Exception handling here
    }
    

    如果你想在你的代码中实现这个,我建议这样做:

    while (true) {
        try {
            rows = scan.nextInt();
            if (rows<5||rows>21) {
                break;
            }
            else {
                System.out.println("That is either out of range or not an integer, try again! ");
            }
        } catch (InputMismatchException ex) {
            System.out.println("That is either out of range or not an integer, try again! ");
        }
    }
    

    更多详情请见here

    【讨论】:

      【解决方案3】:
      String choice2=scan.next();
      if(choice2.equals(y)){
        System.out.println("How many rows/columns(5-21)?");
      try
      {
      rows=scan.nextInt();
      }catch(Exception e)
      {
      rows = -1;
      }
       while(rows<5||rows>21){
        System.out.println("That is either out of range or not an integer, try again! ");
      try
      {
      rows=scan.nextInt();
      }catch(Exception e)
      {
      rows = -1;
      }
      }
      

      【讨论】:

      • -1,你应该很少(如果曾经)直接捕获Exception
      • -1 @Brian S - 在这种情况下,他不在乎异常是什么,如果有异常,他想重新提示用户输入,不管它为什么错,是的是捕获异常的情况你错了!
      • OP 只是在学习,所以一个好的答案会教。 Aeshang 的回答是教如何使用异常的一个很好的例子:它显示了基本语法,以及特定用例的完整语法。在某些情况下使用Exception 很好,但大多数时候它只是糟糕代码的拐杖,这不是你想传递给新程序员的东西。
      猜你喜欢
      • 2014-07-14
      • 2020-08-17
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2023-03-03
      相关资源
      最近更新 更多