Understanding try & catch and error handling

2020-02-13 05:03发布

问题:

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 
}

The code prints what I need it to print, but I also have to have something in the code when it asks how many rows/columns to catch whether or not i input something other than an integer(in the part below). need some help, we haven't done anything yet with how to catch exceptions and i don't know how to start.

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

回答1:

You need to understand this please look into it.

Basic understanding is

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

There is also an finally block which will always be execute even if there is an error. it is used like this

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

In your particular case you can have the following exceptions (link).

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range NoSuchElementException - if input is exhausted IllegalStateException - if this scanner is closed

So you would need to catch exceptions like

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:

You can create a try-catch block like so:

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

If you want to implement this in your code, I suggest doing this:

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! ");
    }
}

See here for more details.



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