I think it is not possible to achieve "Throw a custom exception from a service into action." See Parcel Resource:
public final void readException(int code, String msg) { switch (code) { case EX_SECURITY: throw new SecurityException(msg); case EX_BAD_PARCELABLE: throw new BadParcelableException(msg); case EX_ILLEGAL_ARGUMENT: throw new IllegalArgumentException(msg); case EX_NULL_POINTER: throw new NullPointerException(msg); case EX_ILLEGAL_STATE: throw new IllegalStateException(msg); } throw new RuntimeException("Unknown exception code: " + code + " msg " + msg); }
so that we can know that we can simply throw these five exceptions above.
for example: If your service throws an IllegalArgumentException:
@Override public void addImage(final int align, final byte[] imageData) throws RemoteException { log("/// addImage ///"); if (imageData == null) { throw new IllegalArgumentException("The second argument(image data) can not be empty!"); } ... }
your client will be able to catch him:
try { printer.addImage(0, null); } catch (IllegalArgumentException e) { e.printStackTrace(); }
linlilian
source share