core / pragma.utils / cancellationCheck /
cancellationCheck #
fun CoroutineScope.cancellationCheck(block: () -> Unit)
Checks the cancellation status of the coroutine context. If the coroutine context is active, this function is a no-op. If the coroutine context is cancelled, it runs the block of code and throws a CancellationException. This block cannot contain suspend calls as it is only executed in the case of a cancelling coroutine.
This can be useful when catching Exceptions to handle CancellationExceptions correctly when a coroutine is being cancelled.
Sample Usage: cancellationCheck() {logger.debug("Coroutine cancelled during…")}
Parameters #
block | A non-suspending block of code to run in the case that the coroutine is cancelled |
fun CoroutineContext.cancellationCheck(block: () -> Unit)
Checks the cancellation status of the coroutine context. If the coroutine context is active, this function is a no-op. If the coroutine context is cancelled, runs the block of code and throws a CancellationException. This block cannot contain suspend calls as it is only executed in the case of a cancelling coroutine.
This can be useful when catching Exceptions to handle CancellationExceptions correctly when a coroutine is being cancelled.
Sample Usage: coroutineContext.cancellationCheck() {logger.debug("Coroutine cancelled during…")}
Parameters #
block | A non-suspending block of code to run in the case that the coroutine is cancelled |