title: Kotlin coroutines - gracefully handling errors from suspend functions
body: <p>Trying to implement graceful handling of the errors with suspend functions that are called from async methods, How to catch the error thrown by a suspend method.</p>
  <pre><code>suspend fun findById(id: Long): User? {
      throw Exception(&quot;my exception&quot;) // intentionally throwing to simulate error situation.
      return userModel.findById(id) // IO, may throw an error
  }
  </code></pre>
  <p>Caller piece, launching with IO thread</p>
  <pre><code>GlobalScope.launch(Dispatchers.IO) {
      try {
  
          var userAsync: Deferred&lt;User?&gt;? = null
          arguments?.getLong(&quot;id&quot;)?.let {
              userAsync = async { viewModel?.findById(it) } // async for efficiency as i've other async methods too.
          }
  
          val data = userAsync?.await()
  
          withContext(Dispatchers.Main) {
              user = data // data binding, populating UI fields of user
          }
      } catch (exception: Exception) {
          withContext(Dispatchers.Main) { fault(exception) }
      }
  }
  </code></pre>
  <p>fault method</p>
  <pre><code>private fun fault(exception: Exception) {
      Log.d(&quot;User&quot;, &quot;fault: ${exception.localizedMessage}&quot;) // expecting output
  }
  </code></pre>
  <p>Currently runtime is crashing, want to implement graceful handling of errors.</p>
  <hr />
  <p><strong>Attempt 2</strong></p>
  <p>Tried placing try catch within the async block but it didn't like it.</p>
  <pre><code>var userAsync: Deferred&lt;UserVO?&gt;? = null
  arguments?.getLong(&quot;id&quot;)?.let {
      userAsync = async {
          try {
              delegate?.findById(it)
          } catch (e: Exception) {
              print(e)
          }
      }
  }
  </code></pre>