Concurrency with Coroutines in Android

Jun 5 2024 · Kotlin 1.9.23, Android 14, Android Studio Iguana

Lesson 05: Handle Errors

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Open the starter project in Android Studio. Go to the Lesson5Screen and look at the code. On top, you have the coroutine scope you will use in this lesson. Its context contains several elements.

coroutineScope.launch {
  Log.d(
    "Lesson5",
    "Coroutine inside launch on thread: ${Thread.currentThread().name}",
  )
  throw Exception("launch went wrong")
}
System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
runBlocking {
  Log.d(
    "Lesson5",
    "Coroutine inside runBlocking on thread: ${Thread.currentThread().name}",
  )
  throw Exception("runBlocking went wrong")
}
coroutineScope.async {
  Log.d(
    "Lesson5",
    "Coroutine inside async on thread: ${Thread.currentThread().name}",
  )
  throw Exception("async went wrong")
}
coroutineScope.launch { throw Exception("Exception 1") }
coroutineScope.launch { throw Exception("Exception 2") }
coroutineScope.launch {
  launch {
    try {
      delay(Long.MAX_VALUE)
    } finally {
      throw IllegalArgumentException("Illegal argument exception")
    }
  }
  launch {
    delay(100)
    throw IOException("I/O exception")
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instructions Next: Conclusion