ရိုးရိုးလေး ရှင်းပြမယ်
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 ကိုဦးစားပေးပါ။
- Optional / fallible input
- Pattern + error handling
- Safe value / recovery
Error Handling ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
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 ကို အဆင့်လိုက်ဖတ်မယ်
- Optional / fallible input
enum LoginError: Error {App တစ်ခုမှာ file မတွေ့တာ၊ network failed ဖြစ်တာ၊ payment မအောင်မြင်တာ၊ input မမှန်တာလို runtime issue တွေဖြစ်နိုင်ပါတယ်။ Swift မှာ throw , throws , do-catch , try တွေနဲ့ error ကိုစနစ်တကျကိုင်တွယ်နိုင်ပါတယ်။
- Pattern + error handling
print("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 ကိုဦးစားပေးပါ။
- Safe value / recovery
}Error Handling example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Optional / fallible input
- Pattern + error handling
- 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 ကိုစနစ်တကျကိုင်တွယ်နိုင်ပါတယ်။
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 တစ်ခုခုထဲဝင်ပါတယ်။
Password is incorrectReal-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 ကိုခန့်မှန်းပါ
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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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