receiveCatching

abstract suspend fun receiveCatching(): ChannelResult<E>(source)

Retrieves and removes an element from this channel if it's not empty, or suspends the caller while this channel is empty. This method returns ChannelResult with the value of an element successfully retrieved from the channel or the close cause if the channel was closed. Closed cause may be null if the channel was closed normally. The result cannot be failed without being closed.

This suspending function is cancellable: if the Job of the current coroutine is cancelled while this suspending function is waiting, this function immediately resumes with CancellationException. There is a prompt cancellation guarantee: even if receiveCatching managed to retrieve the element from the channel, but was cancelled while suspended, CancellationException will be thrown. See suspendCancellableCoroutine for low-level details.

Because of the prompt cancellation guarantee, some values retrieved from the channel can become lost. See "Undelivered elements" section in Channel documentation for details on handling undelivered elements.

Note that this function does not check for cancellation when it is not suspended. Use yield or CoroutineScope.isActive to periodically check for cancellation in tight loops if needed.

This function can be used in select invocations with the onReceiveCatching clause. Use tryReceive to try receiving from this channel without waiting.