Skip to content

Coroutines Handle Exception #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kotlin-GDE opened this issue Mar 26, 2019 · 1 comment
Closed

Coroutines Handle Exception #1058

Kotlin-GDE opened this issue Mar 26, 2019 · 1 comment

Comments

@Kotlin-GDE
Copy link

Kotlin-GDE commented Mar 26, 2019

hi

this is a snapshot of my test:

suspend fun getSum(): Int {
    var sum = 0
    repeat(3) {
        delay(500)
        sum += it

        if (it == 2)
            throw RuntimeException("Exception")
    }

    return sum
}

fun main() = runBlocking {

    val res = async {
        getSum()
    }

    val sum = runCatching { res.await() }
    if(sum.isSuccess) println("sum is: ${sum.getOrNull()}") else println("error sum: ${sum.exceptionOrNull()?.message}")

    println("end")
}

i expected with runCatching {..} i avoid exception trace but i already see exception trace still printed and my execution finish with code 1 (expected 0)

whats wrong and how to handle exception with coroutines (without using GlobaleScope)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants