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)?
The javadoc TagsRepresents the relative path to the generated document's root directory from any generated page. Adds a comment indicating that this API should no longer be used. Adds a Throws subheading to the generated documentation, with the classname and description text.
Javadoc (originally cased JavaDoc) is a documentation generator created by Sun Microsystems for the Java language (now owned by Oracle Corporation) for generating API documentation in HTML format from Java source code.
Use the standard style for the Javadoc comment Javadoc only requires a '/**' at the start and a '*/' at the end. In addition to this, use a single star on each additional line: /** * Standard comment. */ public ... /** Compressed comment.
The throws keyword indicates what exception type may be thrown by a method. There are many exception types available in Java: ArithmeticException , ClassNotFoundException , ArrayIndexOutOfBoundsException , SecurityException , etc. Syntax: throw is followed by an object (new type)
There is none, they're synonyms. From the docs:
Documenting Exceptions with
@throws
Tag
NOTE - The tags@throws
and@exception
are synonyms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With