https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

myanmarking

01/29/2020, 4:29 PM
Copy code
@Test
fun `test try-catch outside launch is useless`() = runBlockingTest {
    var result: Int = 0

    try {
        launch {
            throw IllegalStateException()
            result = 1
        }
    } catch (e: Exception) {
        result = 2
    }

    Assert.assertTrue(result == 0)
}

@Test
fun `test try-catch inside launch catch the exception`() = runBlockingTest {
    var result: Int = 0

    launch {
        try {
            throw IllegalStateException()
            result = 1
        } catch (e: Exception) {
            result = 2
        }
    }

    Assert.assertTrue(result == 2)
}
a

Abhishek Bansal

01/29/2020, 4:36 PM
awesome thanks!
2 Views