Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created December 20, 2018 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twyatt/11567581c725c77729e06c9cc33539a9 to your computer and use it in GitHub Desktop.
Save twyatt/11567581c725c77729e06c9cc33539a9 to your computer and use it in GitHub Desktop.
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
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