Stage 1 · Lesson 8 of 10
Error handling
A function that can fail is marked throws. Callers must use try inside a do block with catch clauses, so every place that can fail is visible in the code. This is .
Define your own errors as an that conforms to Error, such as case invalidEmail and case network(Int). try? turns a failure into nil when you do not care why it failed. try! crashes on failure, so avoid it outside tests.
Result wraps a success or a failure as an ordinary value. Catch where the app can respond, for example by showing an alert.
Common traptry? everywhere, which silently throws away the reason something failed.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with
- CourseFree100 Days of SwiftUI
Paul Hudson (Hacking with Swift) · Beginner
Days 1 to 15 teach Swift, with a checkpoint exercise every other day