Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with (cross-process) exceptions in Android custom content provider

I have a custom content provider in my Android app that works reasonably well. I expect other apps to also access my content provider. I would like some clean way to communicate exceptions and errors, but as far as I can tell the Android content provider framework doesn't provide any way to propagate exceptions across processes.

How should I indicate an exception state to my caller? Do I have to somehow encode it into my returned data and rely on clients to check for it? Is there any alternative? If encoding into the ordinary return data is it, what's the best way? (I can see a number of alternatives for the Cursor returned by a query call, but what about the Uri returned by insert, or the int returned by update or delete?)

like image 423
Robert Tupelo-Schneck Avatar asked Aug 15 '12 20:08

Robert Tupelo-Schneck


1 Answers

There is a short list of RuntimeException subclasses which, if thrown in the provider, will be re-thrown in a client app. These include:

  • IllegalStateException
  • IllegalArgumentException
  • NullPointerException
  • SecurityException
  • BadParcelableException

A more recent update to the Parcel.writeException documentation added

  • UnsupportedOperationException
  • NetworkOnMainThreadException

Source: Creating Content Providers mentions IAE and NPE; I guessed that the others would work based on the Javadoc for Parcel.writeException.

The client app will only get the detail message, not a stack trace or the cause stack. For exceptional state which can be encoded into a String (the detail message), this is a reasonable choice.

I'm still interested in other solutions, too.

like image 143
Robert Tupelo-Schneck Avatar answered Sep 29 '22 12:09

Robert Tupelo-Schneck