简体   繁体   English

在javadoc中,标签@throws和@exception之间有什么区别?

[英]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.

相关问题 throws Throwable和throws Exception之间有什么区别 - What is the difference between throws Throwable and throws Exception JavaDoc 中`.` 和`#` 有什么区别? - What is the difference between `.` and `#` in the JavaDoc? 我可以在Javadoc中为同一个异常使用多个@throws标记吗? - Can I use multiple @throws tags for the same exception in Javadoc? “throws”与方法名称和捕获异常有什么区别? - what is difference between “throws” with the method name and catching an exception? try-catch和throws Exception在性能方面有什么区别? - What is the difference between try-catch and throws Exception in terms of performance? throw和throws Exception有什么区别? 为什么“投掷”不需要接球/最终挡住? - What is the difference between throw and throws Exception? And why does “throws” not require a catch/finally block? 关于在 JavaDoc 中对同一异常使用多个 @throws 标签的官方推荐/编码风格指南 - Official recommendation / coding style guide on using multiple @throws tags for the same exception in JavaDoc JavaDoc中的{@code memberData}和<code> memberData </ code>之间有什么区别 - What is the difference between {@code memberData} and <code>memberData</code> in JavaDoc Struts 标签中的 # 、 % 和 $ 符号有什么区别? - What's the difference between # , % and $ signs in Struts tags? 异常处理:Java 中 throw 与 throws 的区别 - Exception handling : Difference between throw vs throws in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM