ensureActive

fun CoroutineScope.ensureActive(): Unit (source)

Ensures that current scope is active. Throws IllegalStateException if the context does not have a job in it.

If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.

This method is a drop-in replacement for the following code, but with more precise exception:

if (!isActive) {
    throw CancellationException()
}
fun Job.ensureActive(): Unit (source)

Ensures that current job is active. If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.

This method is a drop-in replacement for the following code, but with more precise exception:

if (!job.isActive) {
    throw CancellationException()
}