开发者

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

开发者 https://www.devze.com 2023-02-21 14:41 出处:网络
Take the following implementation of a array-based stack of chars for example: 开发者_运维技巧public char peek() throws Underflow {

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


There is none, they're synonyms.

From the docs:

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


@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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消