exceptionOrNull

Returns the exception with which the channel was closed, or null if the channel was not closed or was closed without a cause.

exceptionOrNull can only return a non-null value if isClosed is true, but even if isClosed is true, exceptionOrNull can still return null if the channel was closed without a cause.

val result = channel.tryReceive()
if (result.isClosed) {
// Now we know not to retry the operation later.
// Check if the channel was closed with a cause and rethrow the exception:
result.exceptionOrNull()?.let { throw it }
// Otherwise, the channel was closed without a cause.
}