myungpyo.shim
11/25/2020, 4:39 AMfun main(): Unit = runBlocking {
launch {
//CASE 1 : Will make the application main thread crash
val result = returnAsKotlinResultFunc()
//CASE 2 : Will not make the application main thread crash
// val result = returnAsValueFunc()
println("Return value : $result")
}.join()
}
suspend fun returnAsKotlinResultFunc(): Result<String> = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val result = runCatching {
delay(100)
throw IllegalStateException()
}
result
}
suspend fun returnAsValueFunc(): String = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val result = runCatching {
delay(100)
throw IllegalStateException()
}
"Result : $result"
}
returnAsKotlinResultFunc
makes the application crash. seems like It can't catch the exception. On the other hand, returnAsValueFunc
can normally handle the exception and return the String result.
I think the return type is the only different thing they have.
Have I missed something on it?
(I am using below compiler options to return Result type.
kotlinOptions {
freeCompilerArgs = [
"-Xallow-result-return-type",
//"-Xexplicit-api=strict"
]
}
Val Salamakha
11/25/2020, 9:56 AMtasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-Xallow-result-return-type"
}
}
myungpyo.shim
11/25/2020, 10:43 PMVal Salamakha
11/25/2020, 11:06 PMmyungpyo.shim
11/26/2020, 12:34 AM