简体   繁体   中英

Resume loop after caught exception

I am catching an inputMismatchException in my main method and want my do-while loop to iterate again after the exception is caught. I even coded an explicit continue statement but that didn't work. How can I do so?

public class AddressBookApp {

public static void main(String[] args) {
    AddressBook abook = new AddressBook();

    System.out.println("Welcome to the Address Book Application\n");
    Scanner sc = new Scanner(System.in);

    int menuNumber = 4;
    loop:
    do {
        abook.menu();

        try{
            menuNumber = sc.nextInt();
            System.out.println();

            if (menuNumber < 1 || menuNumber > 4){
                System.out.println("Please enter a valid menu number\n");
            } else if (menuNumber == 1) {
                abook.printEntries();
            } else if (menuNumber == 2) {
                abook.addEntry();
            } else if (menuNumber == 3) {
                abook.removeEntry();
            } else {
                System.out.println("Thanks!  Goodbye.");
                sc.close();
                return;
            }


        } catch (InputMismatchException ime) {
            System.out.println("Please enter an integer");
            sc.next();

            continue loop;
        }

    } while (menuNumber != 4);
    sc.close();
  } 
}

You left menuNumber equal to 4, which is the termination condition of your loop. Of course your loop will end.

You initialized menuNumber to 4, but do not change it in case of an exception. The loop does attempt to continue, but exits because the statement menuNumber != 4 is false.

int menuNumber = 4;
loop:
do {
    abook.menu();

    try{
        menuNumber = sc.nextInt();
        System.out.println();

        if (menuNumber < 1 || menuNumber > 4){
            System.out.println("Please enter a valid menu number\n");
        } else if (menuNumber == 1) {
            abook.printEntries();
        } else if (menuNumber == 2) {
            abook.addEntry();
        } else if (menuNumber == 3) {
            abook.removeEntry();
        } else {
            System.out.println("Thanks!  Goodbye.");
            sc.close();
            return;
        }


    } catch (InputMismatchException ime) {
        System.out.println("Please enter an integer");
        sc.next();

        continue loop;
    }

} while (menuNumber != 4);

Try this

  } catch (InputMismatchException ime) {
    if (fatal(ime)) {
        throw ime;
    } else {
        // try again
        continue;
    }

The loop doesn't continue because an exception of a type OTHER than InputMistmatchException is being thrown. Change the catch to:

catch (Exception e)

or at least add that all encompassing catch condition.

A better solution is to inspect exactly what exception is being thrown and why, and then fix the problem leading to the exception. Having an all encompassing catch with a continue statement could, in theory, lead to an infinite loop because menuNumber is not incremented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM