CoroutineExceptionHandler

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 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:

Types

Key

companion object Key : Key<CoroutineExceptionHandler>

Key for CoroutineExceptionHandler instance in the coroutine context.

Functions

handleException

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

isActive

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

Extension Functions

cancel

fun CoroutineContext.cancel(): Unit

Cancels Job of this context. See Job.cancel for details.

cancelChildren

fun CoroutineContext.cancelChildren(): Unit

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

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