使用 catch 块来继续代码而不是处理异常是否可以?
Is it ok to use a catch block to continue code instead of handling an exception?
我有一些代码涉及检查用户输入以查看输入的输入是字符串还是整数,并将根据结果执行不同的代码。我正在使用 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());
}
}
使用 try/catch 块控制流不被认为是最佳实践,但如果您不关心最佳实践,它是 "acceptable"。
有关检查数字是否为整数的其他方法的示例,请参阅 Determine if a String is an Integer in Java。您可以使用其中一个示例,如果它是整数,则检查它是否等于零。
此外,在您的代码中,您对 Integer.parseInt(dataEntered)
的第二次调用似乎仍会引发不会被捕获的异常。
异常通常只应在特殊情况下使用(看看这个名字从何而来?)。它们在紧密循环中尤其糟糕,因为执行开销很高。无效的用户输入似乎很常见,所以我会寻找另一种方式。 Take a look at this answer.
但这完全取决于语言。例如,在 Python 中,try/catch 是事实上的编码方式(duck-typing)。
我有一些代码涉及检查用户输入以查看输入的输入是字符串还是整数,并将根据结果执行不同的代码。我正在使用 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());
}
}
使用 try/catch 块控制流不被认为是最佳实践,但如果您不关心最佳实践,它是 "acceptable"。
有关检查数字是否为整数的其他方法的示例,请参阅 Determine if a String is an Integer in Java。您可以使用其中一个示例,如果它是整数,则检查它是否等于零。
此外,在您的代码中,您对 Integer.parseInt(dataEntered)
的第二次调用似乎仍会引发不会被捕获的异常。
异常通常只应在特殊情况下使用(看看这个名字从何而来?)。它们在紧密循环中尤其糟糕,因为执行开销很高。无效的用户输入似乎很常见,所以我会寻找另一种方式。 Take a look at this answer.
但这完全取决于语言。例如,在 Python 中,try/catch 是事实上的编码方式(duck-typing)。