0Pricing
Kotlin Multiplatform Academy · Lektion

Netzwerk- und Parsing-Fehler abfangen

Ktor- und Serializer-Exceptions in Ergebnisse umwandeln

Netzwerk- und Parsing-Fehler abfangen ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🛡️

Häufig gestellte Fragen

Ist die Lektion „Netzwerk- und Parsing-Fehler abfangen“ kostenlos?

Ja — der vollständige Text von „Netzwerk- und Parsing-Fehler abfangen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Netzwerk- und Parsing-Fehler abfangen“?

Ktor- und Serializer-Exceptions in Ergebnisse umwandeln Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?

Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Netzwerk- und Parsing-Fehler abfangen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?

Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein Sealed-Result-Typ
  2. Netzwerk- und Parsing-Fehler abfangen
  3. Fehler auf UI-Meldungen abbilden
  4. Retry und Offline-Fallbacks
← Zurück zu Kotlin Multiplatform Academy