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

前端 未结 1 912
野的像风
野的像风 2021-02-04 11:27

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 communica

相关标签:
1条回答
  • 2021-02-04 12:18

    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.

    0 讨论(0)
提交回复
热议问题