了解尝试&捕获和错误处理

时间:2014-09-19 18:31:19

标签: java error-handling try-catch

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

3 个答案:

答案 0 :(得分:2)

您需要了解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");
} 

答案 1 :(得分:1)

您可以像这样创建一个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

答案 2 :(得分:-2)

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