Mobile development

Stage 1 · Lesson 7 of 9

Exceptions and Result

Kotlin fundamentals

has exceptions, but no . The compiler never forces you to catch anything, so you decide where handling makes sense. try returns a value, so val n = try { s.toInt() } catch (e: NumberFormatException) { 0 } works.

Many functions have a safe version that returns null instead of throwing, such as toIntOrNull. runCatching wraps a call in a Result that you can inspect. Use require and check to fail fast on programmer mistakes. code, Kotlin's async code, has one special rule.

Common trapThe trap in coroutine code is catching every Exception, including with runCatching, because that also catches CancellationException and stops a cancelled coroutine from actually stopping.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • GuideFree
    Kotlin Documentation

    JetBrains

    Start with the Kotlin tour, then the null safety and coroutines basics pages

  • CourseFree
    Android Basics with Compose

    Google · Beginner

    The Kotlin pathways in Units 1 to 3 teach the language with practice sets

Practise

  • Dart, TypeScript, Kotlin and Swift tracks with small exercises and free mentor feedback

Keep handy

  • Short Kotlin pages with a try-it editor for checking syntax quickly

Also worth your time

Mark this stage as not done?