0Pricing
Kotlin Multiplatform Academy · บทเรียน

ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์

แปลงข้อยกเว้นจาก Ktor และซีเรียลไลเซอร์เป็นผลลัพธ์

ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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

คำถามที่พบบ่อย

บทเรียน “ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์”

แปลงข้อยกเว้นจาก Ktor และซีเรียลไลเซอร์เป็นผลลัพธ์ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ชนิดผลลัพธ์แบบปิดผนึก
  2. ดักจับข้อผิดพลาดเครือข่ายและการแยกวิเคราะห์
  3. แมปข้อผิดพลาดเป็นข้อความ UI
  4. การลองใหม่และทางเลือกเมื่อออฟไลน์
← กลับไปที่ Kotlin Multiplatform Academy