Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Reactive Programming With Java 8

You're reading from   Learning Reactive Programming With Java 8 Learn how to use RxJava and its reactive Observables to build fast, concurrent, and powerful applications through detailed examples

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781785288722
Length 182 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Nickolay Tzvetinov Nickolay Tzvetinov
Author Profile Icon Nickolay Tzvetinov
Nickolay Tzvetinov
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. An Introduction to Reactive Programming FREE CHAPTER 2. Using the Functional Constructions of Java 8 3. Creating and Connecting Observables, Observers, and Subjects 4. Transforming, Filtering, and Accumulating Your Data 5. Combinators, Conditionals, and Error Handling 6. Using Concurrency and Parallelism with Schedulers 7. Testing Your RxJava Application 8. Resource Management and Extending RxJava Index

Handling errors


When dealing with errors in RxJava, you should be aware that they terminate the Observable chain of actions. Much like with your normal procedural code, once you are in the catch block, you can't go back to the code that has thrown the exception. You can execute some backup logic though and use it instead of failing the program. The return*, retry*, and resume* operators do something similar.

The return and resume operators

The onErrorReturn operator can be used in order to prevent the Subscriber instance's onError from being called. Instead, it will emit one last item and complete. Here is an example:

Observable<String> numbers = Observable
  .just("1", "2", "three", "4", "5")
  .map(Integer::parseInt)
  .onErrorReturn(e -> -1);
  subscribePrint(numbers, "Error returned");

The Integer::parseInt method will succeed in converting the strings 1 and 2 to Integer values, but it will fail on three with a NumberFormatException exception. This exception will be passed to the...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime