exception java doc_在javadoc中,標簽@throws和@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)?

所以我的問題是,兩者之間有什么區別,什么時候應該優先於另一個(例如使用上面的代碼)?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值