我如何捕获多个异常并循环直到获得有效输入?

时间:2019-04-10 16:15:48

标签: java

我必须捕获3个异常并循环,直到输入有效的输入为止。

现在,当输入有效输入时,我的程序结束,但仅在输入无效的情况下不断循环。它从不捕获异常。如果我退出循环并输入说“ m”,它将捕获算术异常,但会为inputMismatch“仅数字”显示错误消息

// Get input from user and perform integer divsion.
do{
    try{
        System.out.print("Please enter a positive value for the numerator: ");
        numerator = keyboard.nextInt();        
        System.out.print("Please enter a positive value for the denominator: ");
        denominator = keyboard.nextInt();
    } 
    catch(InputMismatchException e){
          System.out.println("Enter only numbers.");
    }
    catch(NegativeValueException e){
          System.out.println("no negative values.");
    }
    catch(ArithmeticException e){
          System.out.println("Division by zero.");
    }

    if (numerator >=0 && denominator >= 0){
        quotient = numerator / denominator;
    }else if(numerator<0 && denominator <0){
        throw new NegativeValueException();
    }else
        invalidInput = true;

    System.out.println();
    System.out.print("The result of integer division is: ");
    System.out.println(quotient);
    System.out.println();
}while (invalidInput);  

我只需要捕获异常,并为异常打印出适当的错误消息并循环,直到输入有效的输入为止。现在没有循环,它将捕获一个负值,但不会显示错误并以构建失败结束。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

实际上问题是您在try-block之外进行数学运算,说程序经过以下检查后是否抛出exception

if (numerator >=0 && denominator >= 0) //  false as there was exception
else if(numerator<0 && denominator <0)  // false as there was exception
else
    invalidInput = true; //gets executed

在每个异常invalidInput = true被执行之后,do-while loop被中断。在try-block

中移动数学

尝试以下代码:

do
{
    try
    {
        System.out.print("Please enter a positive value for the numerator: ");
        numerator = keyboard.nextInt();
        System.out.print("Please enter a positive value for the denominator: ");
        denominator = keyboard.nextInt();
        if (numerator >=0 && denominator >= 0)
        {
            quotient = numerator / denominator;
        }
        else if(numerator<0 && denominator <0)
        {
            throw new NegativeValueException();
        }
        else
            invalidInput = true;

        System.out.println();
        System.out.print("The result of integer division is: ");
        System.out.println(quotient);
        System.out.println();
    } 
    catch(InputMismatchException e)
    {
        System.out.println("Enter only numbers.");
    }
    catch(NegativeValueException e)
    {
        System.out.println("no negative values.");
    }
    catch(ArithmeticException e)
    {
        System.out.println("Division by zero.");
    }

}while (invalidInput); 

答案 1 :(得分:0)

由于您已经付出了很多努力,但您似乎对Java还是陌生的,所以我正在尝试更正您的代码。通常,不建议将其用于这种较低的校正级别。

我们需要先引发异常,然后再捕获-NegativeValueException

我假设声明并定义了变量和类型-NegativeValueExceptioninvalidInputkeyboard

我提供了示例代码,对您提出的代码进行了最小的更改,尽管您提出的问题可以通过更好的代码来解决。

do{
        invalidInput=false;
        try{

        System.out.print("Please enter a positive value for the numerator: ");
        int numerator = keyboard.nextInt();

        System.out.print("Please enter a positive value for the denominator: ");
        int denominator = keyboard.nextInt();
        if(numerator<0 && denominator <0){
           throw new NegativeValueException();
         }else if (numerator >=0 && denominator >= 0){
        int quotient = numerator / denominator;
        System.out.println();
        System.out.print("The result of integer division is: ");
        System.out.println(quotient);
        System.out.println();
        }
        } catch(InputMismatchException e){
            System.out.println("Enter only numbers.");
            invalidInput = true;
        }catch(NegativeValueException e){
            System.out.println("no negative values.");
            invalidInput = true;
        }catch(ArithmeticException e){
            System.out.println("Division by zero.");
            invalidInput = true;
        }
        }while (invalidInput);  

答案 2 :(得分:0)

Scanner s = new Scanner(System.in);
do {
    try {
        System.out.print("Please enter a positive value for the numerator: ");
        int numerator = s.nextInt();
        System.out.print("Please enter a positive value for the denominator: ");
        int denominator = s.nextInt();
        int quotient = numerator / denominator;
    } catch (InputMismatchException e) {
        System.out.println("InputMismatchException " + e);
        System.exit(0);


    } catch (ArithmeticException e) {
        System.out.println("ArithmeticException " + e);
        System.exit(0);
    } catch (Exception e) {
        System.out.println("NegativeValueException " + e);
        System.exit(0);
    }
} while (true);