1

I have the following code in my app:

final File f = new File(path);

try {
    boolean folderCreated = f.mkdirs();

    // if dir wasn't created this time, and doesn't exist
    if (!folderCreated && !f.exists()) {
        throw new IOException("Unable to create path");
    }
} catch (SecurityException e) {
            throw new IOException("Unable to create path");
}

When I run findbugs (in Android Studio), it reports Bad Practice Bad use of return value from method Method ignores exceptional return value of java.io.File.mkdirs()

Javadoc states that java.io.File.mkdirs() returns true if the directory was created, or false on failure OR if the directory already existed, hence the !f.exists() check.

I don't want to ignore findbugs messages - not even warnings, but I don't see how I can resolve this as the return value is clearly being checked.

Note, I added the try{...}catch (SecurityException){...} as a suggestion from this post: https://stackoverflow.com/a/23631948/1423896, but it doesn't make any difference.

Community
  • 1
  • 1
Tiksi
  • 471
  • 1
  • 6
  • 15
  • FindBugs 3.0.1 does not report any warnings on this code (because you actually check the `mkdirs` return value). Are you using the latest version? In general sometimes you should ignore FindBugs warnings. FindBugs can produce warnings which actually don't cause any problems in your code. – Tagir Valeev Sep 03 '15 at 15:38
  • Android Studio (IntelliJ build 141.2178183) is reporting `Findbugs 3.0.1`, `FindBugs-IDEA 0.9.997`. findbugs was installed from Settings -> Plugins -> Browse Repositories – Tiksi Sep 03 '15 at 15:59

3 Answers3

3

Maybe the reason is that you invoke method of f instance even if folderCreated is false. Instead, you can invoke it before you invoke f.mkdirs().

boolean folderExisted = f.exists() || f.mkdirs();
if (!folderExisted) {
    throw new IOException("Unable to create path");
}

refs: CheckReturnAnnotationDatabase.java

Kengo TODA
  • 426
  • 4
  • 10
  • It's very strange... I tried what you suggested, but the warning was still present. I rolled back to what I had before, and the message disappeared. I have cleaned/rebuilt/redeployed the project several times between each test, but the findbugs plugin reports inconsistent results. – Tiksi Sep 03 '15 at 15:53
  • @Tiksi, probably some bug in Intellij FindBugs plugin (I don't think it's the problem in FindBugs itself). If you can reproduce such behavior, consider reporting a bug. – Tagir Valeev Sep 03 '15 at 17:28
1

I don't know if it is relevant but it's a good idea to pass the caught exception to the created exception like this (on the last lines):

try {
    boolean folderCreated = f.mkdirs();

    // if dir wasn't created this time, and doesn't exist
    if (!folderCreated && !f.exists()) {
        throw new IOException("Unable to create path");
    }
} catch (SecurityException e) {
    throw new IOException("Unable to create path", e);
}
coffeemakr
  • 330
  • 2
  • 12
0

Use if condition to create a directory. That way you can eliminate findbug error. This works:

if (f.mkdir()) {
    System.out.println("directory " + f+ " created");
}
kalabalik
  • 3,792
  • 2
  • 21
  • 50
Leo
  • 1
  • 5