In javadoc, what is the difference between the tags @throws and @exception?

爷,独闯天下 提交于 2019-12-05 08:34:06

问题


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.

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)?


回答1:


There is none, they're synonyms. From the docs:

Documenting Exceptions with @throws Tag
NOTE - The tags @throws and @exception are synonyms.




回答2:


@throws was added because it is a keyword ("throws" clause in a method declaration),

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:


@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.)



来源:https://stackoverflow.com/questions/5510170/in-javadoc-what-is-the-difference-between-the-tags-throws-and-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!