Error handling for Flux

Earlier, we saw how to check/ verify errors in the stream.

Now let’s see how to handle the error.

Sending a default value in case of error

  • onErrorResume() method can be used to return a default Flux object if any error occurs. The consumer will not know at all that any error occurred.
  • onErrorReturn() method can be used to return a default value instead of the whole Flux.

Transforming an error

  • onErrorMap() method can be used to apply a transformation on the actual Exception object.
  • Notice here that the error is still being thrown. Just the object is transformed from RuntimeException to CustomException.


  • retry() method can be used if we wanted to try again in case of errors.
  • Notice here that the Consumer will only know of an error if the last try throws it. If 1st time or 2nd time error is thrown, the Flux will try again. As the retry count is 2, 3rd time will be the last. If 3rd time also error happens, it will be thrown to Consumer.
  • This can be useful in the case of API calls. If a connection error occurred due to any network problem, we can use the retry() method to try again 2–3 times before throwing an error.
  • In that case, we would ideally want to wait for a while before trying again.
  • retryBackoff() method can be used for that.
  • This method, however, doesn’t throw the original error after all retries. It will throw an Illegal StateException.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store