简体   繁体   English

处理异常的流程

[英]Flow of handling exception

Does anyone have idea why the console output show the menu one more time before printing the exception out? 有谁知道为什么在输出exception之前控制台输出又显示菜单一次?

I except the output should be: 我除了输出应该是:

1. item 1
2. item 2
3. Quit
Please choose a item:
WRONGINPUT         <---- user input
Invalid input      <---- where I want the exception shows
1. item 1
2. item 2
3. Quit
Please choose a item:

However, what I get is: 但是,我得到的是:

1. item 1
2. item 2
3. Quit
Please choose a item:
WRONGINPUT         <---- user input
1. item 1
2. item 2
3. Quit
Please choose a item:
Invalid input      <---- why the exception is shown here?

The code is shown below: 代码如下所示:

    // code omitted

    Scanner scanner = new Scanner(System.in);
    int mainMenu = -1;
    do {    
        try {
            System.out.println("1. item 1");
            System.out.println("2. item 2");
            System.out.println("3. Quit");
            System.out.println("Please choose a item:");
            mainMenu = scanner.nextInt();
        } catch (InputMismatchException e) {
            scanner.nextLine(); 
            System.err.println("Invalid input");        
        }
            if (mainMenu == 1)
                // do something
            else if (mainMenu == 2)
                // do something
            else if (mainMenu == 3)
                System.out.println("Quitting...");
    } while (mainMenu != 3);

控制台输出

Here is answer.Please run this program. 这是答案。请运行此程序。

package java7.demo;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class Test {

    public static void main(String args[]){
        int mainMenu = -1;
         Scanner scanner = new Scanner(System.in);

    do {    
        try {
            System.out.println("1. item 1");
            System.out.println("2. item 2");
            System.out.println("3. Quit");
            System.out.println("Please choose a item:");
            mainMenu = scanner.nextInt();
            if (mainMenu == 1){
                // do something
            }
            else if (mainMenu == 2){

            }
                // do something
            else if (mainMenu == 3){
                System.out.println("Quitting...");
            }else{
                throw new InputMismatchException();
            }
        } catch (InputMismatchException e) {

            System.err.println("Invalid input"); 
            scanner.nextLine(); 
        }

    } while (mainMenu != 3);
    }
}

You only need to change scanner.nextLine() under err print statement. 您只需要在err打印语句下更改scanner.nextLine()。

My code has nothing wrong in the order when I run it in command. 我的代码在命令中运行时顺序没有错。

I find that the reason is System.err.println matters in Eclipse. 我发现原因是System.err.println在Eclipse中很重要。 When I change it to System.out.println , I get correct output order. 当我将其更改为System.out.println ,我得到正确的输出顺序。 But I think it is not necessary as this is the problem with Eclipse. 但是我认为没有必要,因为这是Eclipse的问题。

Console Printing Order in Eclipse Eclipse中的控制台打印顺序

This link gives me the reason. 这个链接给了我原因。 Anyways thanks for your help. 无论如何,谢谢您的帮助。 Cheers 干杯

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

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