ContentResolver.takePersistableUriPermission() has no documented error possible yet evidently can occur

120 Views Asked by At

I've had success using

getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); 

to allow for access to a document across reboots (previously obtained using ACTION_OPEN_DOCUMENT).

But I wonder what happens if it fails as there is no return value and there is no documented exceptions (checked or otherwise) as shown here: docs.

Yet when searching on SO I see there is in fact a SecurityException possible shown here: exception. And it would stand to reason there are errors possible, for example an invalid URI provided - or document no longer exists, etc.

So the question is how to properly handle undocumented RuntimeExceptions which evidently may occur on this call. Are these the only valid approaches?

(A)

try {
    getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); 
} catch (SecurityException e) {
   // handle failure to get persistable permission though NOT DOCUMENTED
}

(B)

try {
    getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); 
} catch (Exception e) {
   // handle failure to get persistable permission
}

And more specifically is there any way to find out all possible RuntimeExceptions from this call ?

1

There are 1 best solutions below

0
CommonsWare On

Are these the only valid approaches?

You could catch Throwable instead of SecurityException or Exception. But, in general, try/catch is how you would catch whatever is thrown.

And more specifically is there any way to find out all possible RuntimeExceptions from this call ?

Not realistically. There are tens of thousands of Android device models, and any of them could contain modifications to Android that affect what gets thrown by the code paths behind takePersistableUriPermission().