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