Javadoc all exceptions

All exceptions thrown by a method can be documented with the @throws javadoc comment (same as @exception). Some argue that an @throws should be included for every thrown exception, since it's the only way to clearly state all requirements made upon the caller (the pre-conditions). However, others disagree.

If all null object parameters passed to class methods cause a NullPointerException, then it's acceptable to state this once in the class javadoc comment, instead of repeating it for every method. Another alternative is to state in overview.html (javadoc's summary description of an application) that all parameters are to be considered as non-null unless explicitly stated otherwise.


This example has an unusually large number of pre-conditions. Remarks regarding NullPointerException are placed in the class level javadoc comment.

Note that the unchecked SecurityException is thrown indirectly, through the calls to the methods of the File class.


* If a null object parameter is passed to any method, then a
* <tt>NullPointerException</tt> will be thrown.
public final class WriteTextFile {

  //..other methods elided

  * Change the contents of a text file in its entirety, overwriting any
  * existing text.
  * @param file is an existing file (not a directory) which can be written.
  * @param contents is the text to be placed into aFile.
  * @exception FileNotFoundException if file does not exist.
  * @exception IOException if stream to file cannot be written to or closed.
  * @exception IllegalArgumentException if file is a directory.
  * @exception IllegalArgumentException if file cannot be written.
  * @exception SecurityException if a SecurityManager exists and
  * disallows read or write access to aFile.
  static public void setContents(File file, String contents)
                                 throws FileNotFoundException, IOException {
    if (file == null) {
      throw new NullPointerException("File must not be null.");
    if (contents == null) {
      throw new NullPointerException("Contents must not be null.");
    if (!file.exists()) {
      throw new FileNotFoundException ("File does not exist: " + file);
    if (!file.isFile()) {
      throw new IllegalArgumentException("Must not be a directory: " + file);
    if (!file.canWrite()) {
      throw new IllegalArgumentException("File cannot be written: " + file);

    try (Writer output = new BufferedWriter(new FileWriter(file))) {

  public static void main (String... args) throws IOException {
    File testFile = new File("C:\\Temp\\blah.txt");
    setContents(testFile, "blah blah blah");

See Also :
Validate method arguments
Avoid @throws in javadoc