[英]In my particular situation should I use custom exceptions or not?
I have to make this student repository where exceptions need to be thrwon if certain conditions are not met.如果不满足某些条件,我必须创建这个学生存储库,其中需要抛出异常。 My question is: should I create my own custom exceptions, or should I use the predefined ones?
我的问题是:我应该创建自己的自定义异常,还是应该使用预定义的异常? I will leave a portion of my code below
我将在下面留下我的一部分代码
public class StudentRepository {
private static List<Student> studentList = new ArrayList<>();
private static Set<Student> studentsByAge = new TreeSet<>(new AgeComparator());
private static Set<Student> studentsByLastName = new TreeSet<>(new LastNameComparator());
public static Student addStudent(String firstName, String lastName, String gender, LocalDate dateOfBirth) {
if (firstName.trim().isEmpty() || lastName.trim().isEmpty()) {
throw new NullPointerException("Last name or first name field may be empty");
} else if (!(gender.equalsIgnoreCase("m") || gender.equalsIgnoreCase("f"))) {
throw new IllegalArgumentException("Gender should be 'M or 'F");
} else if (dateOfBirth.isBefore(LocalDate.of(1900, 1, 1)) ||
dateOfBirth.isAfter(LocalDate.now())) {
throw new IllegalArgumentException("Date of birth should be between 1900 and current year");
}
Student newStudent = new Student(firstName, lastName, dateOfBirth, gender);
studentList.add(newStudent);
return newStudent;
}
public static void deleteStudentByCnp(String cnp) {
Student studentToDelete = null;
for (Student student : studentList) {
if (student.getCnp().equals(cnp)) {
studentToDelete = student;
studentList.remove(studentToDelete);
break;
} else {
throw new NullPointerException("The student does not exist");
}
}
}
You can check Oracle's recommendations on checked vs unchecked exceptions topic which should make it more clear: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html您可以查看 Oracle 关于已检查与未检查异常主题的建议,这应该更清楚: https : //docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
and the summary from the page is the following该页面的摘要如下
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception.
这是底线准则:如果可以合理地期望客户端从异常中恢复,请将其设为已检查异常。 If a client cannot do anything to recover from the exception, make it an unchecked exception.
如果客户端无法从异常中恢复,请将其设为未经检查的异常。
You can also read more about usage of custom exceptions vs standard ones here: https://stackify.com/java-custom-exceptions/您还可以在此处阅读有关自定义异常与标准异常用法的更多信息: https : //stackify.com/java-custom-exceptions/
Generally it depends on client behaviour - what client can do with this exception.通常这取决于客户端的行为 - 客户端可以用这个异常做什么。 If it's only logging error and nothing more - then IllegalArgumentException should be enough but if you then return this error to client, eg using REST - then it would make sense to create custom exception with proper structure that would be serialized and returned in response to keep consistent structure of errors in response.
如果它只是记录错误而仅此而已 - 那么 IllegalArgumentException 应该就足够了,但是如果您随后将此错误返回给客户端,例如使用 REST - 那么创建具有适当结构的自定义异常将是有意义的,该异常将被序列化并返回以响应保持响应错误的一致结构。
Anyway, please do not use NullPointerException
besides cases when something is null unexpectedly (ie it's programmer's error).无论如何,请不要使用
NullPointerException
除了某些意外为 null 的情况(即程序员的错误)。 In your case I would replace NullPointerExceptions with custom exception.在您的情况下,我会用自定义异常替换 NullPointerExceptions。
Assuming these rules (gender must be M
or F
, etc) are considered preconditions (in that the docs call out: The params you pass must adhere to these rules), the correct exception to throw is IllegalArgumentException
.假设这些规则(性别必须是
M
或F
等)被视为先决条件(因为文档指出:您传递的参数必须遵守这些规则),抛出的正确异常是IllegalArgumentException
。 Except for a precondition that a param must not be null, in which case NullPointerException
is appropriate.除了 param 不能为 null 的先决条件,在这种情况下
NullPointerException
是合适的。 Talking about recoverability is not appropriate for that kind of exception - the author of a method has no idea if the caller is the kind of code that can 'reasonably recover from the exception'.谈论可恢复性不适用于这种异常——方法的作者不知道调用者是否是那种可以“合理地从异常中恢复”的代码。 There's a decent chance they can't (say, it's text provided over a network and there's no mechanism to signal back to the caller: Err, you're not following the agreed upon spec there, or it's from a config file), so unchecked is probably better here.
他们很有可能不能(比如,它是通过网络提供的文本,并且没有机制向调用者发回信号:呃,你没有遵循那里商定的规范,或者它来自配置文件),所以在这里未经检查可能更好。
IllegalArgumentException
just fits. IllegalArgumentException
恰到好处。
NullPointerException
, though, is clearly really bad usage here .但是,
NullPointerException
在这里显然是非常糟糕的用法。 That is to signal a thing is null that wasn't expected to be, and that is all , so don't abuse it like this!那就是表示一个事情是空的,这不是预期的,就是这样,所以不要像这样滥用它!
Note that your code has plenty of problems:请注意,您的代码有很多问题:
delete
method will check the first student in the list.delete
方法将检查列表中的第一个学生。 If its CNP matches, that is deleted, and you're done.for
loop.for
循环之外。Map
sounds like a better datastructure here; Map
在这里听起来像是更好的数据结构; currently your delete code will take longer and longer as there are more and more students.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.