Skip to content

Instantly share code, notes, and snippets.

@adesamp
Created July 7, 2021 11:43
Show Gist options
  • Save adesamp/36574f42f80ce51d9228e0e5735a1c58 to your computer and use it in GitHub Desktop.
Save adesamp/36574f42f80ce51d9228e0e5735a1c58 to your computer and use it in GitHub Desktop.
ExceptionHandlingWithAsync.kt
@Test
fun `direct throw exception in async with scope with try-catch`() {
runBlocking {
val deferred: Deferred<String> = GlobalScope.async {
println("start job")
throw IllegalArgumentException("throw IllegalArgumentException from launch")
}
try {
println(deferred.await())
} catch (e: IllegalArgumentException) {
println("catch error : ${e.message}")
}
println("finish")
}
}
@Test
fun `direct throw exception in async with scope with runCatching`() {
runBlocking {
val deferred: Deferred<String> = someScope.async(exceptionHandling) {
println("start job")
throw IllegalArgumentException("throw IllegalArgumentException from launch")
}
runCatching { deferred.await() }
.onFailure { println("catching run: ${it.message}") }
.onSuccess { println(it) }
println("finish")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment