0Pricing
Kotlin Multiplatform Academy · Lesson

Catch Network & Parsing Errors

Convert Ktor and serializer exceptions to results.

Catch Network & Parsing Errors is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Where Errors Begin

Most failures appear at two spots: the network call and the JSON parse. Catching them here keeps the rest of your shared code clean and calm.

Wrap the Risky Call

Put the Ktor request and the decode inside a single try block. One guarded spot is easier to reason about than scattered checks.

try {
  val user = client.get(url).body<User>()
} catch (e: Exception) { }

Catch Network Failures

A dropped connection throws an IOException. Catch it and turn it into a clear Failure your UI can present as a connectivity problem.

catch (e: IOException) {
  Failure("No connection")
}

Catch Parsing Failures

When JSON does not match your model, kotlinx.serialization throws a SerializationException. Catch it separately so you know data, not the network, was bad.

catch (e: SerializationException) {
  Failure("Bad data")
}

Order Your catch Blocks

List the specific exceptions first and a broad Exception last. Kotlin matches top to bottom, so narrow types must come before the catch-all.

Return, Do Not Rethrow

Inside each catch, build a Failure and return it. Rethrowing would just push the crash up to the apps you are trying to protect.

Keep It in commonMain

This guarding belongs in shared code, right around the call. Both Android and iOS then receive the same safe Result with no extra work.

Inspect Ktor Status Codes

A reachable server can still answer 404 or 500. Check the response status and map non-2xx codes to a Failure too.

if (!resp.status.isSuccess())
  return Failure("Server error")

Avoid Swallowing Silently

Never leave an empty catch. Log the cause before returning Failure so you can still debug what really happened on a device.

runCatching as a Shortcut

Kotlin offers runCatching to wrap a block into a Result-like value, a tidy alternative when you do not need custom catch logic.

val r = runCatching { fetch() }

One Funnel, Two Sources

Network and parsing errors now flow through one funnel into your Failure type, so callers handle both with the same simple branch.

Quick Check

Think about how kotlinx.serialization signals trouble.

Recap

You wrapped the call in try, caught IOException and SerializationException separately, mapped bad status codes, and returned tidy Failures from shared code. 🛡️

Frequently asked questions

Is the “Catch Network & Parsing Errors” lesson free?

Yes — the full text of “Catch Network & Parsing Errors” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Catch Network & Parsing Errors”?

Convert Ktor and serializer exceptions to results. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Catch Network & Parsing Errors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. A Sealed Result Type
  2. Catch Network & Parsing Errors
  3. Map Errors to UI Messages
  4. Retry & Offline Fallbacks
← Back to Kotlin Multiplatform Academy