在 Arrays.asList 中计算平均值时创建自定义异常
Creating Custom Exception while calculating the average in the Arrays.asList
我需要找到大学所有学生的平均成绩,并在 Arrays.asList
中找到平均成绩时创建自定义异常。如果 int warCraftGrade
< 0 或 int warCraftGrade
> 10,则应抛出异常。我有以下代码:
public class Student {
String name;
public final int warCraftGrade;
public Student(String name, int warCraftGrade) {
this.name = name;
this.warCraftGrade = warCraftGrade;
}
public String getName() {
return name;
}
public int getWarCraftGrade() {
return warCraftGrade;
}
我有学生名单:
static List<Student> students = Arrays.asList(
new Student("Geoffrey Baratheon", 8),
new Student("Barristan Selmy", 6) //and so on
);
以及取平均值的方法:
double warCraftAverageGrade = students.stream()
.mapToDouble(Student::getWarCraftGrade)
.average()
.getAsDouble();
我创建了特殊例外 class:
public class GradeException extends Exception{
public GradeException() {
}
public GradeException(String message) {
super(message);
}
public GradeException(String message, Throwable cause) {
super(message, cause);
}
和class定义异常方法:
public class StudentActions {
public static void range (Student student) throws GradeException {
if (student.warCraftGrade < 0 || student.warCraftGrade > 10) {
throw new GradeException("Wrong grade");
}
}
}
当我尝试使用 StudentActions.range()
方法时出现问题:
public static void main(String[] args) {
try {
StudentActions.range();
double warCraftAverageGrade = students.stream()
.mapToDouble(Student::getWarCraftGrade)
.average()
.getAsDouble();
System.out.println("Average grade in WarCraft for the entire university = " + warCraftAverageGrade);
} catch (GradeException e) {
System.out.println("Grade is out of range (0-10)");
}
在这种情况下形成自定义异常的正确解决方案是什么?例如,如果成绩为负数,则正确的代码必须抛出 GradeException
:
//new Student("Jorah Mormont", -8)
提前致谢!
最好将异常移动到构造函数中,而不是每次 获取平均值时都必须捕获异常。简单地让你不能构造一个无效的对象。在您的特定情况下,抛出已检查的异常(不扩展 RuntimeException
并附加到 throws
子句,迫使调用者处理它的异常)没有意义。
This answer 建议何时适合使用检查异常(强调我的):
I also think that throwing checked exceptions can be OK1, assuming that the checked exception is 1) declared, 2) specific to the problem you are reporting, and 3) it is reasonable to expect the caller to deal with a checked exception for this2.
2 - For example, the existing FileInputStream constructors will throw FileNotFoundException if you try to open a file that does not exist. Assuming that it is reasonable for FileNotFoundException to be a checked exception3, then the constructor is the most appropriate place for that exception to be thrown. If we threw the FileNotFoundException the first time that (say) a read or write call was made, that is liable to make application logic more complicated.
此外,我建议将实际成绩范围逻辑移动到一个方法中,而不是强制调用者每次都这样做。
最后一件事:我会让你的方法成为非静态的,因为你正在处理一个实例。在 Java(我不知道你来自什么语言)中,this
,当前实例在所有非静态方法中都可用,并且优于将实例传递给静态方法。
看看这些问题:
- When to throw an exception?
- Is it good practice to make the constructor throw an exception?
我需要找到大学所有学生的平均成绩,并在 Arrays.asList
中找到平均成绩时创建自定义异常。如果 int warCraftGrade
< 0 或 int warCraftGrade
> 10,则应抛出异常。我有以下代码:
public class Student {
String name;
public final int warCraftGrade;
public Student(String name, int warCraftGrade) {
this.name = name;
this.warCraftGrade = warCraftGrade;
}
public String getName() {
return name;
}
public int getWarCraftGrade() {
return warCraftGrade;
}
我有学生名单:
static List<Student> students = Arrays.asList(
new Student("Geoffrey Baratheon", 8),
new Student("Barristan Selmy", 6) //and so on
);
以及取平均值的方法:
double warCraftAverageGrade = students.stream()
.mapToDouble(Student::getWarCraftGrade)
.average()
.getAsDouble();
我创建了特殊例外 class:
public class GradeException extends Exception{
public GradeException() {
}
public GradeException(String message) {
super(message);
}
public GradeException(String message, Throwable cause) {
super(message, cause);
}
和class定义异常方法:
public class StudentActions {
public static void range (Student student) throws GradeException {
if (student.warCraftGrade < 0 || student.warCraftGrade > 10) {
throw new GradeException("Wrong grade");
}
}
}
当我尝试使用 StudentActions.range()
方法时出现问题:
public static void main(String[] args) {
try {
StudentActions.range();
double warCraftAverageGrade = students.stream()
.mapToDouble(Student::getWarCraftGrade)
.average()
.getAsDouble();
System.out.println("Average grade in WarCraft for the entire university = " + warCraftAverageGrade);
} catch (GradeException e) {
System.out.println("Grade is out of range (0-10)");
}
在这种情况下形成自定义异常的正确解决方案是什么?例如,如果成绩为负数,则正确的代码必须抛出 GradeException
:
//new Student("Jorah Mormont", -8)
提前致谢!
最好将异常移动到构造函数中,而不是每次 获取平均值时都必须捕获异常。简单地让你不能构造一个无效的对象。在您的特定情况下,抛出已检查的异常(不扩展 RuntimeException
并附加到 throws
子句,迫使调用者处理它的异常)没有意义。
This answer 建议何时适合使用检查异常(强调我的):
I also think that throwing checked exceptions can be OK1, assuming that the checked exception is 1) declared, 2) specific to the problem you are reporting, and 3) it is reasonable to expect the caller to deal with a checked exception for this2.
2 - For example, the existing FileInputStream constructors will throw FileNotFoundException if you try to open a file that does not exist. Assuming that it is reasonable for FileNotFoundException to be a checked exception3, then the constructor is the most appropriate place for that exception to be thrown. If we threw the FileNotFoundException the first time that (say) a read or write call was made, that is liable to make application logic more complicated.
此外,我建议将实际成绩范围逻辑移动到一个方法中,而不是强制调用者每次都这样做。
最后一件事:我会让你的方法成为非静态的,因为你正在处理一个实例。在 Java(我不知道你来自什么语言)中,this
,当前实例在所有非静态方法中都可用,并且优于将实例传递给静态方法。
看看这些问题:
- When to throw an exception?
- Is it good practice to make the constructor throw an exception?