title: Kotlin coroutine can't handle exception
body: <p>I was playing around with coroutines and found some very strange behavior. I want to convert some asynchronous requests in my project using <code>suspendCoroutine()</code>. Here's piece of code showing this problem.</p>
  
  <p>In first case, when suspend function is being called in <code>runBlocking</code> coroutine, exception from continuation goes to catch block, and then <code>runBlocking</code> finishes successfully. But in second case, when creating new <code>async</code> coroutine, exception goes through catch block and crashes the whole program.</p>
  
  <pre><code>package com.example.lib
  
  import kotlinx.coroutines.async
  import kotlinx.coroutines.runBlocking
  import kotlin.coroutines.resumeWithException
  import kotlin.coroutines.suspendCoroutine
  
  object Test {
      fun runSuccessfulCoroutine() {
          runBlocking {
              try {
                  Repository.fail()
              } catch (ex: Throwable) {
                  println("Catching ex in runSuccessfulCoroutine(): $ex")
              }
          }
      }
  
      fun runFailingCoroutine() {
          runBlocking {
              try {
                  async { Repository.fail() }.await()
              } catch (ex: Throwable) {
                  println("Catching ex in runFailingCoroutine(): $ex")
              }
          }
      }
  }
  
  object Repository {
      suspend fun fail(): Int = suspendCoroutine { cont -&gt;
          cont.resumeWithException(RuntimeException("Exception at ${Thread.currentThread().name}"))
      }
  }
  
  
  fun main() {
      Test.runSuccessfulCoroutine()
      println()
  
      Test.runFailingCoroutine()
  
      println("We will never get here")
  }
  </code></pre>
  
  <p>That's what is printed on console: </p>
  
  <pre><code>Catching ex in runSuccessfulCoroutine(): java.lang.RuntimeException: Exception at main
  
  Catching ex in runFailingCoroutine(): java.lang.RuntimeException: Exception at main
  Exception in thread "main" java.lang.RuntimeException: Exception at main
      at com.example.lib.Repository.fail(MyClass.kt:32)
      at com.example.lib.Test$runFailingCoroutine$1$1.invokeSuspend(MyClass.kt:22)
      at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
      at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
      at kotlinx.coroutines.EventLoopBase.processNextEvent(EventLoop.kt:123)
      at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:69)
      at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45)
      at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
      at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35)
      at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
      at com.example.lib.Test.runFailingCoroutine(MyClass.kt:20)
      at com.example.lib.MyClassKt.main(MyClass.kt:41)
      at com.example.lib.MyClassKt.main(MyClass.kt)
  
  Process finished with exit code 1
  </code></pre>
  
  <p>Any ideas why this is happening - is it a bug, or am i using coroutines the wrong way?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Using <code>coroutineScope { ... }</code> will mitigate problem in <code>runFailingCoroutine()</code></p>
  
  <pre><code>fun runFailingCoroutine() = runBlocking {
      try {
          coroutineScope { async { fail() }.await()  }
      } catch (ex: Throwable) {
          println("Catching ex in runFailingCoroutine(): $ex")
      }
  }
  </code></pre>