简体   繁体   English

异常处理在运行程序中只能运行一次

[英]Exception Handling works only once in running program

Initially I tried to put the try statement into the while loop, however, I was encountered with several errors. 最初我试图将try语句放入while循环中,但是,遇到了几个错误。 The program runs perfectly except when I input a irregular character once it gives me the Printed line I inputted I created, however, when I inputted another one again, the line does not pop up and rather gives me a format exception error. 该程序运行完美,除非我输入一个不规则字符,一旦它给我输入我创建的打印行,但是,当我再次输入另一个时,该行不会弹出,而是给我一个格式异常错误。

AddNumbersrealone2.java AddNumbersrealone2.java

import java.io.*;

// create the class
public class AddNumbersrealone2
{
    // allows i/o
  public static void main (String [] args) throws IOException
  {   // initalize variables and strings
    BufferedReader myInput = new BufferedReader  (new InputStreamReader (System.in));
    String sumNumbers;
    //String go;
    double num ;
    double total = 0.0;

    // asks user questions and instructions
    System.out.println("Hello, the following program will ask for your input of a number ");
    System.out.println("Each time you input a number a running total will be added to each previous number")  ;
    System.out.println("Once ready input a number to start!");
    // try and catch block      
    try {

      num = 0;

      // while statement if this occurs stop the program, in this case if a negative integer is inputted

      while (num >= 0) {

        // Contious question asked  
        System.out.println("Input another number..."); 
        sumNumbers = myInput.readLine();
        num = Double.parseDouble (sumNumbers);

        // calculates number (Running total)
        total = total + num;
        System.out.println(total);

        // end error trap
      }
    }
    catch  (Exception e){
      System.out.println("Please refrain from entering regular characters!");
      num = 0;

      // re- while statement if this occurs stop the program, in this case if a negative integer is inputted

      while ( num >= 0) {

        // input question after a character is inputted
        System.out.println("Please input a number: ");
        sumNumbers = myInput.readLine();
        num = Double.parseDouble (sumNumbers);

        total = total + num;
        System.out.println(total);

        // ending statement
      }
    }
    System.out.println("You entered a negative number, the program will exit now");
    System.out.println("Good-bye!");

    // Complete class body
  }
}

You want something to catch the exception around the Double.parseDouble 你想要一些东西来捕捉Double.parseDouble周围的异常

for example. 例如。

 while(num >= 0)
    {
    // input question after a character is inputted
       System.out.println("Please input a number: ");
       sumNumbers = myInput.readLine();
       try{
            num = Double.parseDouble (sumNumbers);
            total = total + num;
            System.out.println(total);
       } catch(Exception e)
       {
            System.out.println("Please enter a proper number");
       }    


            // ending statement
  }

Your problem is that as soon as the first exception is thrown, your program becomes caught up inside the while() loop inside of your catch statement. 您的问题是,只要抛出第一个异常,您的程序就会陷入catch语句内的while()循环中。 Thus, if another invalid input is entered, it is dealt with in that second while loop where you have no try-catch statement. 因此,如果输入了另一个无效输入,则在第二个while循环中处理它,其中没有try-catch语句。 A good fix would be to have your try statement encompass only the line where you say num = Double.parseDouble (sumNumbers);. 一个很好的解决方法是让你的try语句只包含你所说的num = Double.parseDouble(sumNumbers);的行。 When you catch the exception, end it with a continue; 捕获异常时,以continue结束; statement so that your program loops back the beginning and asks for another input. 声明,以便您的程序循环回头,并要求另一个输入。

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

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