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)?
所以我的问题是,两者之间有什么区别,什么时候应该优先于另一个(例如使用上面的代码)?
3 个解决方案
#1
44
There is none, they're synonyms. From the docs:
没有,他们是同义词。来自文档:
Documenting Exceptions with
@throws
Tag
NOTE - The tags@throws
and@exception
are synonyms.使用@throws标记异常标记注 - 标记@throws和@exception是同义词。
#2
11
@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
否则,两者都是同义词
#3
5
@exception
isn't 100% correct if you code throws a Throwable
. @throws
is more exact. (I realize there isn't a good use case for ever using throw new Throwable()
, but theoretically it is allowed.)
如果代码抛出Throwable,则@exception不是100%正确。 @throws更准确。 (我意识到使用throw new Throwable()没有一个好的用例,但理论上它是允许的。)
#1
44
There is none, they're synonyms. From the docs:
没有,他们是同义词。来自文档:
Documenting Exceptions with
@throws
Tag
NOTE - The tags@throws
and@exception
are synonyms.使用@throws标记异常标记注 - 标记@throws和@exception是同义词。
#2
11
@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
否则,两者都是同义词
#3
5
@exception
isn't 100% correct if you code throws a Throwable
. @throws
is more exact. (I realize there isn't a good use case for ever using throw new Throwable()
, but theoretically it is allowed.)
如果代码抛出Throwable,则@exception不是100%正确。 @throws更准确。 (我意识到使用throw new Throwable()没有一个好的用例,但理论上它是允许的。)