So I've written a Service and an Activity for the Android OS.
My service is running in it's own process, so all the communication between my Activities and the Service happens via IPC. I use the standard Android .aidl mechanism for this.
So far everything works fine. However, the AIDL generates all method stubs using "throws RemoteException" so I have to handle them.
I did a quick grep on the entire Android source-code and only found three cases where this exception is ever thrown. These are in a different service that I don't connect with.
I checked the C-sources as well because in theory RemoteExceptions can be generated using the JNI interface.. Nothing turned up.
I have the impression that everyone just handles them like this:
try {
mService.someMethodCall (someArguments);
} catch (RemoteException e) {
e.printStackTrace();
}
This is not solid code, and I don't want something like this in my code-base.
Besides that: I tried to throw a RemoteException via IPC myself and all I got was a stack-trace and a system log message that tells me that exceptions aren't supported yet. My application never saw the exception and the services that threw the exception ended up in a very strange state (halfway working) :-(
The questions are:
Do these exceptions ever get thrown?
Has anyone ever seen such a try-catch block catching a RemoteException?
Could it be that they don't exist and that we are just forced to deal with them because the "throws RemoteException" is dead code or a left-over inside the AIDL compiler?
Disclamer: I haven't read the entire source-code. I used Grep to find the occurrences of RemoteException, so I may have missed some due to different whitespace usage.
These exceptions do indeed get thrown and you should write appropriate try/catch logic to handle the situation where a remote method you invoked on a service did not complete.
As far as your investigation, you were on the right track looking through the native sources. What you may have overlooked is that
android.os.RemoteException
is a actually just a base class for other Binder related exceptions and that it is a subclass,android.os.DeadObjectException
, which is thrown within the native code of Binder.An activity will see this exception if it makes use of a service running in another process that dies in the middle of performing a request. I was able to prove this to myself by making the following minor changes to Marko Gargenta's AIDLDemo example.
First, make sure the service runs in its own process by updating the AndroidManifest.xml:
Then modify the
add
method to exit prematurely:In logcat you see the service process die, the activity receive a
DeadObjectException
, and ultimately the system respawn the service process.I would imagine if your service was running in the same process as your activity you might never see this exception but then again if that were the case you probably wouldn't be bothering with AIDL.
Additionally, as you discovered, Android does not tunnel exceptions between processes. If you need to communicate an error back to a calling activity then you need to use other means.