Undelivered Errors

Learn about undelivered errors and how to handle them.

There are some cases where errors might be considered to be undelivered. Examples include if an Exception occurs but no explicit .onError() handling was provided on the subscription, or if .onError() was provided but the subscription has been canceled or disposed of. In these scenarios, RxJava tries to do its best to make sure that errors are not lost or swallowed, but instead are reported in a reasonable manner.

Handling undelivered errors

The RxJavaPlugins.onError() method is invoked in RxJava internals to handle the undelivered error. Essentially, this default .onError() behaves as we would expect if the error occurred outside of the context of RxJava. The error’s stack trace is printed out and handling is delegated to the UncaughtExceptionHandler of the Thread where the error occurred.

Alternatively, a default error notification consumer can also be provided to handle undelivered errors via RxJavaPlugins.setErrorHandler(Consumer<Throwable>). Doing so sets a static variable within RxJava so that undelivered errors are propagated to the provided handler as opposed to using RxjavaPlugins.onError(). This approach is preferred since we can pipe the error into our app’s logging module.

Get hands-on with 1200+ tech skills courses.