Understanding try & catch and error handling

前端 未结 3 1393
我在风中等你
我在风中等你 2020-12-20 07:25
import java.util.Scanner;

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

        int rows=0;
              


        
相关标签:
3条回答
  • 2020-12-20 07:45

    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.

    0 讨论(0)
  • 2020-12-20 07:54
    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;
    }
    }
    
    0 讨论(0)
  • 2020-12-20 07:55

    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");
    } 
    
    0 讨论(0)
提交回复
热议问题