[英]In javadoc, what is the difference between the tags @throws and @exception?
Take the following implementation of a array-based stack of chars for example: 以下面的基于数组的字符堆栈的实现为例:
public char peek() throws Underflow {
if (!isEmpty()) {
return stack[pos];
} else {
throw new Underflow("Peeking at an empty stack.");
}
}
Back when I'm using just a text editor I always use the @exception tag, but now my IDE (Netbeans) used @throws when generating the javadoc. 回到我只使用文本编辑器时,我总是使用@exception标签,但现在我的IDE(Netbeans)在生成javadoc时使用了@throws。
So my question is, what is the difference between the two and when should one be preferred over another (using the above code for example)? 所以我的问题是,两者之间的差异是什么时候应该优先于另一个(例如使用上面的代码)?
There is none, they're synonyms. 没有,他们是同义词。 From the docs :
来自文档 :
Documenting Exceptions with
@throws
Tag使用
@throws
标记记录异常
NOTE - The tags@throws
and@exception
are synonyms.注 - 标签
@throws
和@exception
是同义词。
@throws
was added because it is a keyword ("throws" clause in a method declaration), 添加了
@throws
,因为它是一个关键字(方法声明中的“throws”子句),
and, as a verb, it is just more natural to read. 而且,作为一个动词,阅读起来更自然。 This reads as a sentence:
这读作一句话:
@throws NullPointerException
while this seem more redundant: 虽然这似乎更多余:
@exception NullPointerException
Otherwise, both are synonyms 否则,两者都是同义词
@exception
isn't 100% correct if you code throws a Throwable
. 如果您的代码抛出
Throwable
则@exception
不是100%正确。 @throws
is more exact. @throws
更准确。 (I realize there isn't a good use case for ever using throw new Throwable()
, but theoretically it is allowed.) (我意识到使用
throw new Throwable()
没有一个好的用例,但理论上它是允许的。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.