【问题标题】:Is it ok to use a catch block to continue code instead of handling an exception?可以使用 catch 块来继续代码而不是处理异常吗?
【发布时间】:2015-07-18 14:57:01
【问题描述】:

我有一些代码涉及检查用户输入以查看输入的输入是字符串还是 int,并且会根据结果执行不同的代码。我正在使用 Integer.parseInt 来确定用户输入是否为整数,如果不是则抛出 NumberFormatException。

但是,为了控制代码流,我使用了 try/catch 语句,catch 块用于包含在用户输入是字符串(即 NumberFormatException)时将运行的代码.

Qn

这是使用 try/catch 块的可接受方式吗?我试过用谷歌搜索这个,但我能找到的只是用来处理抛出的异常而不是像我一样使用的 catch 块的例子。

import java.io.*;
import java.util.*;

public class Datafile{

    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Kence\\workspace\\Java 8 - Beyond the Basics - Working Files\\Practice Programs\\src\\Practice Data Edited",true));
        String data = null;
        boolean end = false;
        boolean cont = true;
        String dataEntered = null;
        int menuChoice = printMenu();
        while(!end){
            switch(menuChoice){
                case 1:
                    System.out.println("Please enter a line of data or press 0 to exit back to the main menu");
                    dataEntered = input.nextLine();
                    try {
                        if(Integer.parseInt(dataEntered) == 0){
                            break;
                        }
                    } catch (Exception e) {
                        data += dataEntered + "\n";
                        while(cont){
                            System.out.println("Data entered.Please enter the next line of data or press quit to exit back to the main menu.");
                            dataEntered = input.nextLine();
                            if(Integer.parseInt(dataEntered) == 0){
                                cont = false;
                                break;
                            }else{
                                data+= dataEntered;
                                System.out.println("Current data entered is " + dataEntered);
                            }
                    }


                    }


                    break;

                case 2:
                    System.out.println("2 Entered");
                    break;
                case 3:
                    System.out.println("3 Entered");
                    break;
                case 4:
                    System.out.println("4 Entered");
                    break;
            }//End of switch statement
            menuChoice = printMenu();
        }



        input.close();
    }//End of main



    public static void printStars(){
        for(int i = 0; i<66 ; i++){
            System.out.print("*");
        }
        System.out.println();
    }

    public static int printMenu(){

        printStars();
        System.out.println("System Started");
        printStars();
        System.out.println("Enter 1 to input a new line of data");
        System.out.println("Enter 2 to list all data");
        System.out.println("Enter 3 to save existing data");
        System.out.println("Enter 4 to load data");
        printStars();
        return Integer.parseInt(input.nextLine());
    }

}

【问题讨论】:

    标签: java logic try-catch


    【解决方案1】:

    将 try/catch 块用于控制流不是最佳做法,但如果您不关心最佳做法,它是“可以接受的”。

    请参阅Determine if a String is an Integer in Java 以了解其他检查数字是否为整数的方法示例。您可以使用其中一个示例,然后如果它是整数,则检查它是否等于零。

    此外,在您的代码中,您对Integer.parseInt(dataEntered) 的第二次调用似乎仍可能引发不会被捕获的异常。

    【讨论】:

      【解决方案2】:

      异常通常只应在特殊情况下使用(看看名称的来源?)。它们在紧密循环中尤其糟糕,因为执行开销很高。无效的用户输入似乎很常见,所以我会寻找另一种方法。 Take a look at this answer.

      但这一切都取决于语言。例如,在 Python 中,try/catch 是事实上的编码方式(duck-typing)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多