Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-661

FileUtils throws inconsistent exceptions

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.6
    • Fix Version/s: 2.9.0
    • Component/s: Utilities
    • Labels:
      None

      Description

      The FileUtils throws inconsistent exceptions, when an input is not a directory. 

      The copyFileToDirectory throws IllegalArgumentException:

       

        public static void copyFileToDirectory(final File srcFile, final File destDir, final boolean preserveFileDate)
                  throws IOException {
              if (destDir == null) {
                  throw new NullPointerException("Destination must not be null");
              }
              if (destDir.exists() && destDir.isDirectory() == false) {
                  throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
              }
              final File destFile = new File(destDir, srcFile.getName());
              copyFile(srcFile, destFile, preserveFileDate);
          }
      
      

      The other methods throw IOException:

       

       

       public static void moveFileToDirectory(final File srcFile, final File destDir, final boolean createDestDir)
                  throws IOException {
              validateMoveParameters(srcFile, destDir);
              if (!destDir.exists() && createDestDir) {
                  destDir.mkdirs();
              }
              if (!destDir.exists()) {
                  throw new FileNotFoundException("Destination directory '" + destDir +
                          "' does not exist [createDestDir=" + createDestDir + "]");
              }
              if (!destDir.isDirectory()) {
                  throw new IOException("Destination '" + destDir + "' is not a directory");
              }
              moveFile(srcFile, new File(destDir, srcFile.getName()));
          }
      

      I feel that IOException is more reasonable. Although the messages are the same, the different exception can cause problems when programmers catch the thrown exceptions. 

       

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                haozhong Hao Zhong
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 40m
                  1h 40m