Thuta Learning
Swift
AdvancedProgrammingအခြေခံ

Error Handling

Swiftသင်ခန်းစာ 2525 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Error Handling ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Nil, error, boundary နဲ့concurrency cases စမ်းနိုင်ရန်
  • Safe, testable Swift code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Error Handling ကို ရိုးရိုးလေးနားလည်မယ်

App တစ်ခုမှာ file မတွေ့တာ၊ network failed ဖြစ်တာ၊ payment မအောင်မြင်တာ၊ input မမှန်တာလို runtime issue တွေဖြစ်နိုင်ပါတယ်။ Swift မှာ throw , throws , do-catch , try တွေနဲ့ error ကိုစနစ်တကျကိုင်တွယ်နိုင်ပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

try လိုတဲ့ function ကို do-catch မပါဘဲခေါ်ရင် compile error ဖြစ်နိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Error Handling မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

Mental Model
  1. Optional / fallible input
  2. Pattern + error handling
  3. Safe value / recovery

Error Handling ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

swift
enum LoginError: Error {
    case emptyUsername
    case wrongPassword
}

func login(username: String, password: String) throws {
    if username.isEmpty {
        throw LoginError.emptyUsername
    }

    if password != "123456" {
        throw LoginError.wrongPassword
    }

    print("Login success")
}

do {
    try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
    print("Username is required")
} catch LoginError.wrongPassword {
    print("Password is incorrect")
} catch {
    print("Something went wrong")
}

Error Handling မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Optional / fallible inputenum LoginError: Error {

    App တစ်ခုမှာ file မတွေ့တာ၊ network failed ဖြစ်တာ၊ payment မအောင်မြင်တာ၊ input မမှန်တာလို runtime issue တွေဖြစ်နိုင်ပါတယ်။ Swift မှာ throw , throws , do-catch , try တွေနဲ့ error ကိုစနစ်တကျကိုင်တွယ်နိုင်ပါတယ်။

  2. Pattern + error handlingprint("Login success")

    Error Handling မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

  3. Safe value / recovery}

    Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Execution Flow
  1. Optional / fallible input
  2. Pattern + error handling
  3. Safe value / recovery

Success

Error Handling ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

try လိုတဲ့ function ကို do-catch မပါဘဲခေါ်ရင် compile error ဖြစ်နိုင်ပါတယ်။

App တစ်ခုမှာ file မတွေ့တာ၊ network failed ဖြစ်တာ၊ payment မအောင်မြင်တာ၊ input မမှန်တာလို runtime issue တွေဖြစ်နိုင်ပါတယ်။ Swift မှာ throw, throws, do-catch, try တွေနဲ့ error ကိုစနစ်တကျကိုင်တွယ်နိုင်ပါတယ်။

swift
enum LoginError: Error {
    case emptyUsername
    case wrongPassword
}

func login(username: String, password: String) throws {
    if username.isEmpty {
        throw LoginError.emptyUsername
    }

    if password != "123456" {
        throw LoginError.wrongPassword
    }

    print("Login success")
}

do {
    try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
    print("Username is required")
} catch LoginError.wrongPassword {
    print("Password is incorrect")
} catch {
    print("Something went wrong")
}

login function က error ဖြစ်နိုင်လို့ throws ထည့်ထားပါတယ်။ Function ကိုခေါ်တဲ့အခါ try လိုပြီး error ဖြစ်ရင် catch block တစ်ခုခုထဲဝင်ပါတယ်။

You should see
Password is incorrect

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
swift
enum LoginError: Error {
    case emptyUsername
    case wrongPassword
}

func login(username: String, password: String) throws {
    if username.isEmpty {
        throw LoginError.emptyUsername
    }

    if password != "123456" {
        throw LoginError.wrongPassword
    }

    print("Login success")
}

do {
    try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
    print("Username is required")
} catch LoginError.wrongPassword {
    print("Password is incorrect")
} catch {
    print("Something went wrong")
}

ရလဒ်: Error Handling ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Quick Quiz 1 / 3

Error Handling ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Optional / fallible input → Pattern + error handling → Safe value / recovery
  • Error Handling မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
  • try လိုတဲ့ function ကို do-catch မပါဘဲခေါ်ရင် compile error ဖြစ်နိုင်ပါတယ်။
  • Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

The Swift Programming Language — Swift.org

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • try လိုတဲ့ function ကို do-catch မပါဘဲခေါ်ရင် compile error ဖြစ်နိုင်ပါတယ်။
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Force unwrap သို့unchecked shared mutable state ဖြင့်compiler safety ကိုကျော်ခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Password is incorrect

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Error Handling | Thuta Learning