Created
December 20, 2018 23:45
-
-
Save twyatt/11567581c725c77729e06c9cc33539a9 to your computer and use it in GitHub Desktop.
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
This file contains hidden or 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
import kotlinx.coroutines.channels.Channel | |
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED | |
import kotlinx.coroutines.channels.ClosedReceiveChannelException | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
class SourceClosed(cause: Throwable) : IllegalStateException(cause) | |
fun main(args: Array<String>) = runBlocking<Unit> { | |
val channel = Channel<Int>(CONFLATED) | |
launch { | |
try { | |
channel.receive() | |
} catch (e: ClosedReceiveChannelException) { | |
throw SourceClosed(e) | |
} | |
} | |
launch { | |
delay(1_000L) | |
channel.cancel() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment