如何在Java中使用错误处理/异常处理



所以我试图在我的程序中实现错误捕捉器,但我不能100%确定它们的实际工作方式,我在YouTube上看了一些教程,但它们向我解释的方式并不是100%清楚,因此,我在这里真正想做的是,试图在代码的结尾添加一些东西,如果你插入一个他无法识别的字母或数字,他不会给我们一个错误,但他会给我们一条信息,告诉我们出了什么问题。

我试图用if语句制作一个更容易的版本,但我认为这并不完全适合这段代码,因为我必须用if语句重写所有内容,而且我不确定实际如何工作。

我认为可以工作的代码

它不让我把代码放在这里:C

我试图实现错误处理的代码

import java.util.Scanner;
public class Main {
public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
System.out.println("Insert number of students: ");
String student = sc.nextLine();
int studenti = Integer.parseInt(student);
int[] array = new int[studenti];
for (int i = 0; i<studenti;i++)
{
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();

array[i]= Integer.parseInt(score);

}

System.out.println("nPercentages are: ");
int sum = 0; // Added a variable for sum
for (int i = 0; i < studenti; i++) {
System.out.println((array[i]) + "%");
sum += array[i]; // maintaining sum
}
int average = sum / array.length; // calculating the average
System.out.println("nThe Average Score is: " + average + "%");

}

使用try/catch块。

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
System.out.println("Insert number of students: ");
String student = sc.nextLine();
try{   
int studenti = Integer.parseInt(student);
int[] array = new int[studenti];
for (int i = 0; i<studenti;i++)
{
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();

array[i]= Integer.parseInt(score);

}

System.out.println("nPercentages are: ");
int sum = 0; // Added a variable for sum
for (int i = 0; i < studenti; i++) {
System.out.println((array[i]) + "%");
sum += array[i]; // maintaining sum
}
int average = sum / array.length; // calculating the average
System.out.println("nThe Average Score is: " + average + "%");
}catch(Exception ex){
System.out.println("Error, details: " + ex.Message());
}

您可以使用许多"Exception",例如numberFormatException。

错误处理其实并没有那么复杂。这是关于知道在try语句的作用域中抛出了什么类型的异常。当您将光标放在正在使用的方法上时,IDE通常可以获得这些信息。

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Insert number of students: ");
int student = sc.nextInt(); // note Im using a method specific for int, instead of parsing
int[] array = new int[student];
for (int i = 0; i < student; i++) {
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();
array[i]= Integer.parseInt(score);
}
System.out.println("nPercentages are: ");
int sum = 0;
for (int i = 0; i < student; i++) {
System.out.println((array[i]) + "%");
sum += array[i];
}
int average = sum / student; // student == array.length
System.out.println("nThe Average Score is: " + average + "%");
} catch (InputMismatchException | NumberFormatException exception) {  
// This will catch errors due wrong input (ie user entered a letter 
// instead of a digit) and execute the code within this block.
// Another possible Exception thrown is IllegalStateException.
System.err.println("There was an error. Details:");
exception.printStackTrace(System.err); // remove if you don't want to see the error trace
} finally { // this will get executed whether your code had errors or not
sc.close(); // dont forget to close your scanner
}
}

如果您的IDE没有向您显示有关方法的信息,则可能没有对其进行文档记录,或者您没有导入所述文档。对于这种情况,只需谷歌一下,你就会发现例外情况。例如,此链接向您显示有关Scanner.nextInt()和Integer.parseInt(。的信息

代码中损坏的部分除以零。处理异常

try {
int average = sum / array.length; // throw Exception
}
catch (ArithmeticException e) {
System.out.println(
"Divided by zero operation cannot possible");
}

最新更新