Created
July 7, 2021 11:43
-
-
Save adesamp/36574f42f80ce51d9228e0e5735a1c58 to your computer and use it in GitHub Desktop.
ExceptionHandlingWithAsync.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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