ရိုးရိုးလေး ရှင်းပြမယ်
Project: Task Manager App - Part 3 (Polish + Wrap Up) ကို ရိုးရိုးလေးနားလည်မယ်
Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်
ဖြစ်လေ့ရှိတဲ့အမှား
catch block တွေရေးတဲ့အခါ specific case ကို အရင်မစစ်ဘဲ general catch { } ကို အပေါ်ဆုံးမှာ ထားလိုက်လို့ specific catch code ဘယ်တော့မှ run မဖြစ်တတ်ကြတယ်
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
enum TaskError: Error {
case emptyTitle
case taskNotFound(id: Int)
}
extension TaskManager {
func addTask(title: String, priority: Priority) throws {
guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
throw TaskError.emptyTitle
}
let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
tasks.append(newTask)
}
func completeTask(id: Int) throws {
guard let index = tasks.firstIndex(where: { $0.id == id }) else {
throw TaskError.taskNotFound(id: id)
}
tasks[index].isDone = true
}
func printSummary() {
print("===== Summary =====")
print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
for p in [Priority.high, .medium, .low] {
let count = tasks.filter { $0.priority == p && !$0.isDone }.count
print("\(p.rawValue) pending: \(count)")
}
}
}
do {
try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
print("Unknown error: \(error)")
}
manager.printSummary()Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Project requirements
enum TaskError: Error {Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။
- Components + state + services
print("===== Summary =====")Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
- စမ်းသပ်ပြီး UI
manager.printSummary()Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Project requirements
- Components + state + services
- စမ်းသပ်ပြီး UI
Success
Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်
ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
TaskError enum ကို emptyTitle, taskNotFound(id: Int) case နှစ်ခုနဲ့ Error protocol conform လုပ်ပြီး define ပါ။ addTask(title:priority:) ကို throws function ပြောင်းပြီး title.isEmpty ဖြစ်ရင် TaskError.emptyTitle throw ပါ။ completeTask(id:) ကိုလည်း throws ပြောင်းပြီး matching task မတွေ့ရင် TaskError.taskNotFound throw ပါ။ printSummary() method ရေးပြီး total, done, pending count နဲ့ priority တစ်ခုချင်းစီရဲ့ pending count ကို print ထုတ်ပါ။ main code ထဲမှာ do-catch block သုံးပြီး error case နှစ်ခုစလုံးကို handle ပြီး error message user-friendly ဖြစ်အောင် print ပါ။
Code နမူနာ
enum TaskError: Error {
case emptyTitle
case taskNotFound(id: Int)
}
extension TaskManager {
func addTask(title: String, priority: Priority) throws {
guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
throw TaskError.emptyTitle
}
let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
tasks.append(newTask)
}
func completeTask(id: Int) throws {
guard let index = tasks.firstIndex(where: { $0.id == id }) else {
throw TaskError.taskNotFound(id: id)
}
tasks[index].isDone = true
}
func printSummary() {
print("===== Summary =====")
print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
for p in [Priority.high, .medium, .low] {
let count = tasks.filter { $0.priority == p && !$0.isDone }.count
print("\(p.rawValue) pending: \(count)")
}
}
}
do {
try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
print("Unknown error: \(error)")
}
manager.printSummary()id #99 ကို ရှာမတွေ့လို့ "Task id #99 ကို ရှာမတွေ့ပါ" ဆိုတဲ့ message print ထွက်ပြီး၊ printSummary() ကနေ priority အလိုက် pending count ပါတဲ့ report ကို ထုတ်ပေးပါမယ်။၅ မိနစ် စမ်းကြည့်
TaskError မှာ duplicateTitle case တစ်ခု ထပ်ထည့်ပြီး addTask မှာ title တူညီတဲ့ task ရှိပြီးသားဆိုရင် error throw ဖြစ်အောင် ကိုယ်တိုင် ရေးကြည့်ပါ- 5 minute လောက် စမ်းကြည့်ပါ။
သတိလေးတစ်ချက်
catch block တွေရေးတဲ့အခါ order အရေးကြီးပါတယ် - general catch { } ကို အမြဲနောက်ဆုံးမှာပဲ ထားရပါမယ်၊ မဟုတ်ရင် specific error case တွေကို ဘယ်တော့မှ မရောက်ဘဲ compile error တောင် ထွက်နိုင်ပါတယ်။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Project: Task Manager App - Part 3 (Polish + Wrap Up) 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 TaskError: Error {
case emptyTitle
case taskNotFound(id: Int)
}
extension TaskManager {
func addTask(title: String, priority: Priority) throws {
guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
throw TaskError.emptyTitle
}
let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
tasks.append(newTask)
}
func completeTask(id: Int) throws {
guard let index = tasks.firstIndex(where: { $0.id == id }) else {
throw TaskError.taskNotFound(id: id)
}
tasks[index].isDone = true
}
func printSummary() {
print("===== Summary =====")
print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
for p in [Priority.high, .medium, .low] {
let count = tasks.filter { $0.priority == p && !$0.isDone }.count
print("\(p.rawValue) pending: \(count)")
}
}
}
do {
try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
print("Unknown error: \(error)")
}
manager.printSummary()ရလဒ်: Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Project: Task Manager App - Part 3 (Polish + Wrap Up) 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
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Project requirements → Components + state + services → စမ်းသပ်ပြီး UI
- Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
- throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်
- Project: Task Manager App - Part 3 (Polish + Wrap Up) 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