不能多次捕获异常

时间:2016-10-25 16:00:47

标签: java exception-handling

当运行代码时,如果我输入除number之外的任何字符,则会捕获异常“java.util.InputMismatchException”,但是如果我输入除number之外的任何字符,程序将被终止,并显示错误“Exception in thread“main”java.util.InputMismatchException“。如何使它能够捕获多个同时发生的异常,直到给出有效的输入。

/*
Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the
student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses
in an integer array and determine the frequency of each rating.
 */
import java.util.Scanner;

public class StudentPoll 
{
    static Scanner input=new Scanner(System.in);
    public static void main(String[] args)
    {
        System.out.println("Rate the quality of food from 1 to 5." +
                "\n1 being “awful” and 5 being “excellent”.\n");
        int[] array=new int[10];
        int num =0;
        for(int i=0;i<array.length;i++)
        {

            do{
                System.out.println("Student "+(i+1)+" Enter Your Response:");
                try
                {
                    num=input.nextInt();
                }
                catch(java.util.InputMismatchException e)
                {
                    System.out.println("Enter numbers only.");
                    input.nextLine();
                    num=input.nextInt();
                }   
                if(num<=0 || num>5)

                {
                    System.out.println("Enter 1 to 5 only.");
                }
            }while(num<=0 || num>5);
            array[i]=num;

        }

        int[] frequency=new int[6];

        for ( int i = 0; i < array.length; i++ )
        {
            frequency[array[i]]=frequency[array[i]]+1;      
        }

        System.out.printf("*    :%d (awful)\n",frequency[1]);
        System.out.printf("**   :%d\n",frequency[2]);
        System.out.printf("***  :%d\n",frequency[3]);
        System.out.printf("**** :%d\n",frequency[4]);
        System.out.printf("*****:%d (excellent)\n",frequency[5]);
    }
}`

3 个答案:

答案 0 :(得分:0)

因为你的catch语句中没有另一个try-catch语句,所以如果你想再次捕获异常,你可以重新运行循环。所以你要替换:

catch(java.util.InputMismatchException e)
{
    System.out.println("Enter numbers only.");
    input.nextLine();
    num = input.nextInt();
} 

使用:

catch(java.util.InputMismatchException e)
{
    System.out.println("Enter numbers only.");
    input.nextLine();
    continue;
}

答案 1 :(得分:0)

因为当catch块捕获的try块中发生第一个异常时。如果用户再次给出无效输入,则再次抛出异常。但是代码中没有机制来捕获该异常。因此主线程停止了。 Catch块不负责捕获该块内部抛出的异常。

import java.util.Scanner;

public class StudentPoll
{
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{
    System.out.println("Rate the quality of food from 1 to 5." +
            "\n1 being “awful” and 5 being “excellent”.\n");
    int[] array=new int[10];
    int num =0;
    for(int i=0;i<array.length;i++)
    {

        do{
            System.out.println("Student "+(i+1)+" Enter Your Response:");
            try
            {
                num=input.nextInt();
            }
            catch(java.util.InputMismatchException e)
            {
                input.next(); // consume the leftover new line
                System.out.println("Enter numbers only.");
            }
            if(num<=0 || num>5)

            {
                System.out.println("Enter 1 to 5 only.");
            }

        }while(num<1 || num>5); // change in the logic
        array[i]=num;

    }

    int[] frequency=new int[6];

    for ( int i = 0; i < array.length; i++ )
    {
        frequency[array[i]]=frequency[array[i]]+1;
    }

    System.out.printf("*    :%d (awful)\n",frequency[1]);
    System.out.printf("**   :%d\n",frequency[2]);
    System.out.printf("***  :%d\n",frequency[3]);
    System.out.printf("**** :%d\n",frequency[4]);
    System.out.printf("*****:%d (excellent)\n",frequency[5]);
}
}  

答案 2 :(得分:0)

您的问题陈述是在错误输入之后,程序不会等待下一个用户输入 你可以通过java.util.Scanner&amp; amp;来接受输入。在do while中制作循环。

根据您的需要尝试以下代码。

/*
Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the
student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses
in an integer array and determine the frequency of each rating.
 */
import java.util.Scanner;

public class StudentPoll  {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Rate the quality of food from 1 to 5."
                + "\n1 being “awful” and 5 being “excellent”.\n");
        int[] array = new int[10];
        int num = 0;

        do {
            for (int i = 0; i < array.length; i++) {
                System.out.println("Student " + (i + 1)
                        + " Enter Your Response:");
                Scanner sc = new Scanner(System.in);
                try {
                    num = sc.nextInt();
                } catch (java.util.InputMismatchException e) {
                    System.out.println("Enter numbers only.");
                    num = 0;
                    --i;
                    continue;
                }
                if (num <= 0 || num > 5) {
                    System.out.println("Enter 1 to 5 only.");
                    num = 0;
                    --i;
                    continue;
                }
                array[i] = num;
            }
        } while (num <= 0 || num > 5);


        int[] frequency = new int[6];

        for (int i = 0; i < array.length; i++) {
            frequency[array[i]] = frequency[array[i]] + 1;
        }

        System.out.printf("*    :%d (awful)\n", frequency[1]);
        System.out.printf("**   :%d\n", frequency[2]);
        System.out.printf("***  :%d\n", frequency[3]);
        System.out.printf("**** :%d\n", frequency[4]);
        System.out.printf("*****:%d (excellent)\n", frequency[5]);
    }
}