Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4365451

java.io.FileWriter constructors does not throw IOException

XMLWordPrintable

    • generic, sparc
    • generic, solaris_2.6



      Name: ooR10001 Date: 08/24/2000


      java.io.FileWriter constructors does not throw java.io.IOException in case when
      the specified file is not found, they just creates the file instead.

      For example, javadoc spec for java.io.FileWriter(java.io.File, boolean) states:
      --------------------
      public FileWriter(File file, boolean append)
                 throws IOException

      --- cut

            Throws:
                  IOException - if the specified file is not found or if some other I/O
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  error occurs
            Since:
                  1.4

      --------------------

      Test to repoduce a bug:
      --------------------------
      import java.io.*;

      public class test {

          public static void main(String[] args) {
              try {
              
                  File file = new File("ttt");
                  FileWriter fw = new FileWriter(file, true);
                  fw.close();
                  
              } catch (IOException e) {
                  e.printStackTrace();
                  System.out.println("OKAY. IOException exception was thrown");
                  return;
              }
              System.out.println("FAILED. IOException was not thrown.");
          }

      }
      ---------------------------

      Test output:

      ------------------
      FAILED. IOException was not thrown.
      ------------------

      This is common behaviour of all java.io.FileWriter constructors

      ======================================================================

      Name: yyT116575 Date: 08/16/2001


      The documentation of the java.lang.io.FileWriter constructors contains
      the following description of the IOException that may be thrown:

      Throws: IOException - if the specified file is not found or if some other I/O error occurs.

      (http://java.sun.com/j2se/1.4/docs/api/java/io/FileWriter.html)

      Since FileWriter is in most cases creating a new file, the first part of the
      IOException description ("the specified file is not found") does not apply here. Such description is even more confusing in the FileWriter(String, boolean) constructor. Will the constructor throw if FileWriter("foo.txt", true) is called and "foo.txt" does not exist? Or will it succeed by creating "foo.txt" in the filesystem?

      It appears that the IOException description has been "copied & pasted" across the constructor JavaDoc comments, and hence some duplication and confustion has been introduced.
      (Review ID: 130108)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            oovsunw Oov Oov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: