How to catch RemoteServiceException?

When a service is started with startForegroundService, the service class also has to execute a startForeground. If the service class does not do this, there will be a RemoteServiceException, such as

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

My question is, how can I catch this exception? I tried placing the try/catch statement around the startForeground command, but that didn’t catch the exception. Any thoughts?

I don’t know Android, but this sounds like the exception is being thrown from another thread.

This is one of those cases where the full stack trace is useful: scan down it (i.e. up the call stack) for classes you recognise — or if none, then the bottom of the trace (i.e. the top-level class) should indicate which thread the exception is being thrown from. Hopefully that’ll give an idea as to how to handle it.