Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created April 15, 2021 08:11
Show Gist options
  • Save twyatt/ebfa8172bf2a5c37a479864f66834db5 to your computer and use it in GitHub Desktop.
Save twyatt/ebfa8172bf2a5c37a479864f66834db5 to your computer and use it in GitHub Desktop.
Exceptions thrown when sending to closed/cancelled Channel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
suspend fun main() = coroutineScope<Unit> {
val receive = Channel<Int>()
receive.cancel() // ReceiveChannel
// java.util.concurrent.CancellationException: RendezvousChannel was cancelled
runCatching { receive.send(1) }.onFailure { println(it) }
val send = Channel<Int>()
send.close() // SendChannel
// kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed
runCatching { send.send(2) }.onFailure { println(it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment