You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2021/01/18 20:46:00 UTC

[jira] [Commented] (IO-661) FileUtils throws inconsistent exceptions

    [ https://issues.apache.org/jira/browse/IO-661?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17267521#comment-17267521 ] 

Gary D. Gregory commented on IO-661:
------------------------------------

[~haozhong]

Please see the current git master which contains more consistent exceptions. If your use case is covered, please close this ticket.

TY.

> FileUtils throws inconsistent exceptions
> ----------------------------------------
>
>                 Key: IO-661
>                 URL: https://issues.apache.org/jira/browse/IO-661
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.6
>            Reporter: Hao Zhong
>            Priority: Major
>          Time Spent: 1h 40m
>  Remaining Estimate: 0h
>
> The FileUtils throws inconsistent exceptions, when an input is not a directory. 
> The copyFileToDirectory throws IllegalArgumentException:
>  
> {code:java}
>   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);
>     }
> {code}
> The other methods throw IOException:
>  
>  
> {code:java}
>  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()));
>     }
> {code}
> 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. 
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)