JavaDoc 中是否应该描述 unchecked Exception?
Should unchecked Exception be described in JavaDoc?
我有以下代码:
public User getUserById(Long id) {
checkUserExists(id);
return repo.findOne(id);
}
private void checkUserExists(Long id) {
if (id == null || !repo.exists(id)) {
throw new NoUserFoundException("No User exists with id: " +id);
}
}
根据Oracle:
"Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary."
我是否必须在 JavaDoc 中描述异常(没有 @throws
子句,但只描述?)在 JavaDoc 中描述这种未经检查的异常的最佳方式是什么?
您正在为您的方法的用户编写 Javadoc。如果该用户知道它可能会抛出异常是有用的,请将其记录下来!
在您的情况下,让用户知道如果找不到用户则抛出 NoUserFoundException
似乎确实很有用。
在其他情况下,它的用处不大。例如,在许多情况下,Javadoc 中没有记录参数为 null 时抛出 NullPointerException
的事实,因为它通常以某种方式暗示参数不能为 null。
顺便说一句,Oracle 说的是 throws
class 出现在方法声明之后,而不是 Javadoc。如果您决定记录一个非检查异常,使用 @throws
子句是有意义的。
我有以下代码:
public User getUserById(Long id) {
checkUserExists(id);
return repo.findOne(id);
}
private void checkUserExists(Long id) {
if (id == null || !repo.exists(id)) {
throw new NoUserFoundException("No User exists with id: " +id);
}
}
根据Oracle:
"Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary."
我是否必须在 JavaDoc 中描述异常(没有 @throws
子句,但只描述?)在 JavaDoc 中描述这种未经检查的异常的最佳方式是什么?
您正在为您的方法的用户编写 Javadoc。如果该用户知道它可能会抛出异常是有用的,请将其记录下来!
在您的情况下,让用户知道如果找不到用户则抛出 NoUserFoundException
似乎确实很有用。
在其他情况下,它的用处不大。例如,在许多情况下,Javadoc 中没有记录参数为 null 时抛出 NullPointerException
的事实,因为它通常以某种方式暗示参数不能为 null。
顺便说一句,Oracle 说的是 throws
class 出现在方法声明之后,而不是 Javadoc。如果您决定记录一个非检查异常,使用 @throws
子句是有意义的。