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.)
精彩评论