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()沒有一個好的用例,但理論上它是允許的。)