1

I work with File.java class. Most of its methods can throw SecurityException. But I don't find any information about cases in which it throws.

I look in android sources and find following: different File methods perform such call

 SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.e(path);
    }

Or checkWrite() / checkDelete() In all this cases SecurityManager creates FilePermission object and validates it in Context.checkPermission()

public void checkRead(String file, Object context) {
    checkPermission(new FilePermission(file, "read"), context);
}

Actually from this code I don't figure out when for current Context some file operation will deny and SecurityException will thrown (except trivial situation with not declared in manifest permissions). I guess it also thrown when app try to get access to protected directories, like "root" or "date". But when else?

Oleksii Kolotylenko
  • 1,021
  • 1
  • 8
  • 9

1 Answers1

1

SecurityExceptions are for when you try to do something that your app does not have permission to do. i.e. writing to the SD card without WRITE_EXTERNAL permission listed in your manifest.

As far as I know the lack of permission in manifest is the only way to cause a SecurityException to be raised. However it might be worth noting that anytime you are dealing with file i/o there is also the posssibility of other Exceptions, like IOException.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156