title: How to handle exceptions thrown by observer's onNext in RxJava?
body: <p>Consider the following example:</p>
  
  <pre><code>Observable.range(1, 10).subscribe(i -&gt; {
      System.out.println(i);
  
      if (i == 5) {
          throw new RuntimeException("oops!");
      }
  }, Throwable::printStackTrace);
  </code></pre>
  
  <p>This outputs numbers from 1 to 5 and then prints the exception.</p>
  
  <p>What I want to achieve is make the observer stay subscribed and continue to run after throwing an exception, i.e. print all numbers from 1 to 10.</p>
  
  <p>I have tried using <code>retry()</code> and <a href="https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators" rel="nofollow noreferrer">other various error handling operators</a>, but, as said in the documentation, their purpose is handling errors emitted by the observable itself.</p>
  
  <p>The most straightforward solution is just to wrap the whole body of <code>onNext</code> into a try-catch block, but that doesn't sound like a good solution to me. In the <a href="https://stackoverflow.com/q/7210051/759064">similar Rx.NET question</a>, the proposed solution was to make an extension method which would do the wrapping by creating a proxy observable. I tried to remake it:</p>
  
  <pre><code>Observable&lt;Integer&gt; origin = Observable.range(1, 10);
  Observable&lt;Integer&gt; proxy = Observable.create((Observable.OnSubscribe&lt;Integer&gt;) s -&gt;
          origin.subscribe(i -&gt; {try { s.onNext(i); } catch (Exception ignored) {}}, s::onError, s::onCompleted));
  
  proxy.subscribe(i -&gt; {
      System.out.println(i);
  
      if (i == 5) {
          throw new RuntimeException("oops!");
      }
  }, Throwable::printStackTrace);
  </code></pre>
  
  <p>This does not change anything, because RxJava itself wraps the subscriber into a <a href="http://reactivex.io/RxJava/javadoc/rx/observers/SafeSubscriber.html" rel="nofollow noreferrer"><code>SafeSubscriber</code></a>. Using <code>unsafeSubscribe</code> to get around it doesn't seem to be a good solution either.</p>
  
  <p>What can I do to solve this problem?</p>