title: Rx Exception Handling Fail
body: <p>I'm using RxJava with a retrofit to make API calls,</p>
  
  <p>By using RxJava methods like <strong>flatMap</strong> and <strong>map</strong> I'm making API calls as well as performing DB operations in room database on the background thread.</p>
  
  <p>My implementation is perfect and working fine if there is no error, but In the case when I got an error or any exception while performing DB Operation, Application getting crashed due to following Rx error.</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
  
  The exception was not handled due to missing onError handler in the subscribe() method call.
  </code></pre>
  
  <p>I have used RxJava to perform my operation as below :</p>
  
  <pre><code>mDataManager.login(params)
                  .flatMap { loginResponse: LoginResponse -&gt;
  
                      // here making another API call based on previos API result
                      return@flatMap mDatamanager....
                  }
                  .flatMap { object: SomeDataModel -&gt;
  
                      // here inserting data to DB
                      mDataManager.insertDataToDB(object).subscribe()
  
                      // here making another API call based on previos API 
                      return@flatMap mDataManager...
                  }.map {
  
                      // here inserting data to DB
                      mDataManager.insertDataToDB(object).subscribe()
  
                      return@map true
                  }
                  .observeOn(mSchedulerProvider.ui())
                  .subscribeOn(mSchedulerProvider.io())
                  .subscribe({ result -&gt;
                      // updating view
                  }, { throwable -&gt;
                      throwable.printStackTrace()
                  })
  </code></pre>
  
  <p>I'm getting an exception while inserting data to DB </p>
  
  <p>Exception <code>java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:</code></p>
  
  <p>But the error not handled and Application getting crash.</p>
  
  <p>The error says that <code>missing onError handler in the subscribe() method</code> but in my code, I already override throwable to handle exception/errors.</p>
  
  <p>Can anyone find, what I'm missing or what mistake I have done in code.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Found the solution, Mistake was here : </p>
  
  <p><code>mDataManager.insertDataToDB(object).subscribe()</code></p>
  
  <p>In the Above statement, I'm subscribing but was not handling the error for that and because of that error was not handled by rxJava and in the result, the application gets crashed.</p>
  
  <p>Final Code as below :</p>
  
  <pre><code>mDataManager.login(params)
              .flatMap { loginResponse: LoginResponse -&gt;
  
                  // here making another API call based on previos API result
                  return@flatMap mDatamanager....
              }
              .flatMap { object: SomeDataModel -&gt;
  
                  // avoid this inner subscribe
                  // mDataManager.insertDataToDB(object).subscribe()
  
                  return@flatMap mDataManager.insertDataToDB(object)
              }
              .flatMap {
                  // here making another API call based on previos API result
                  return@flatMap mDatamanager....
              }
              .flatMap {
  
                  // avoid this inner subscribe
                  //mDataManager.insertDataToDB(object).subscribe()
  
                  return@flatMap mDataManager.insertDataToDB(object)
              }
              .observeOn(mSchedulerProvider.ui())
              .subscribeOn(mSchedulerProvider.io())
              .subscribe({ result -&gt;
                  // updating view
              }, { throwable -&gt;
                  throwable.printStackTrace()
              })
  </code></pre>
  
  <p>Above code is working Fine!</p>