简体   繁体   English

捕获异常后继续循环

[英]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. 我在主方法中捕获了inputMismatchException,并希望在捕获异常后再次进行do-while循环。 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. 您留下的menuNumber等于4,这是循环的终止条件。 Of course your loop will end. 当然,您的循环将结束。

You initialized menuNumber to 4, but do not change it in case of an exception. 您已将menuNumber初始化为4,但是在发生异常的情况下请勿将其更改。 The loop does attempt to continue, but exits because the statement menuNumber != 4 is false. 该循环确实尝试继续,但是由于语句menuNumber != 4为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. 该循环不会继续,因为抛出了InputMistmatchException以外的OTHER类型的异常。 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. 从理论上讲,使用continue语句包含所有包含的内容可能会导致无限循环,因为menuNumber不会增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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