interface CoroutineExceptionHandler : Element (source)

An optional element in the coroutine context to handle uncaught exceptions.

Normally, uncaught exceptions can only result from coroutines created using the launch builder. A coroutine that was created using async always catches all its exceptions and represents them in the resulting Deferred object.

By default, when no handler is installed, uncaught exception are handled in the following way:



companion object Key : Key<CoroutineExceptionHandler>

Key for CoroutineExceptionHandler instance in the coroutine context.



abstract fun handleException(
    context: CoroutineContext,
    exception: Throwable
): Unit

Handles uncaught exception in the given context. It is invoked if coroutine has an uncaught exception.

Extension Properties


Returns true when the Job of the coroutine in this context is still active (has not completed and was not cancelled yet).

Extension Functions


fun CoroutineContext.cancel(
    cause: CancellationException? = null
): Unit

Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.


fun CoroutineContext.cancelChildren(
    cause: CancellationException? = null
): Unit

Cancels all children of the Job in this context, without touching the state of this job itself with an optional cancellation cause. See Job.cancel. It does not do anything if there is no job in the context or it has no children.


fun CoroutineContext.ensureActive(): Unit

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