🚀 Enrollments Open for Jetpack Compose Cohort 2 — 7 Days of Live Learning to Build Modern Android UIs 💚Join Now
KotlinIntermediate4 min
Exception Handling in Coroutine?

Answer

Handling exceptions in coroutines depends on the scope and builder.

1. try-catch

The most basic way. Works inside `launch` or `async` (when you call await).

```kotlin launch { try { doWork() } catch (e: Exception) { handleError(e) } } ```

2. CoroutineExceptionHandler

A context element to handle uncaught exceptions in `launch`.

  • Note: Does NOT catch exceptions in `async`.
  • Note: Only works in the root coroutine or a SupervisorJob.

```kotlin val handler = CoroutineExceptionHandler { _, exception -> println("Caught $exception") } scope.launch(handler) { throw RuntimeException("Boom") } ```

3. Async / Await

Exceptions in `async` are deferred until you call `.await()`. You must wrap the `await()` call in try-catch.

Want to master these concepts?

Join our live cohorts and build production-ready Android apps.

Accelerate Your Growth

Don't just learn concepts in isolation. Build production-ready Android apps with expert guidance.

Live Interactive Sessions
Code Reviews & Feedback
Real-world Projects
Career Guidance

Limited seats available for next cohort