I have never gotten this error before so I am not sure what to do or what it means
Unhandled exception type
OperationApplicationException
It occurs in this code:
public void putSettings(SharedPreferences pref){
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE,"vnd.android.cursor.item/color")
.withValue("data1",nColor).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); //error
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE,"vnd.android.cursor.item/vibrate")
.withValue("data1", nVibrate).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); //error
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE, "vnd.android.cursor.item/sound")
.withValue("data1", ringTonePath).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);//error
}
It gives me 2 options “add throws declaration” and “surround with try/catch”.
What do I have to do and why?
It means a method you’re calling is declared with the
throwsdirective for an exception derived from theExceptionclass. When a method is declared in this way, you are forced to deal with the exception using atry/catchblock or add an identicalthrows(for the same exception or a super type) statement to your method declaration.An example.
I want to call some method
fooinside my methodbar.Here is
foo‘s definition:I want to call
foo. If I simply do this:…then I’ll get the error you are experiencing.
foodeclares to the world that it really might decide to throw anExceptionand you had better be ready to deal with it.I have two options. I can change
bar‘s definition as follows:Now I’ve publicized my own warning that my method or some method I call could throw an
Exceptionthat the user of my method should deal with. Since I’ve deferred responsibility to my method’s caller, my method doesn’t have to deal with the exception itself.It’s often better to deal with the exception yourself, if you can. That brings us to the second option, the
try/catch:Now I’ve dealt with the potential
Exceptionthrown byfoothat the compiler was complaining about. Since it’s been dealt with, I don’t need to add athrowsdirective to mybarmethod.