Mobile development

Stage 1 · Lesson 8 of 10

Error handling

Swift fundamentals

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

  • CourseFree
    100 Days of SwiftUI

    Paul Hudson (Hacking with Swift) · Beginner

    Days 1 to 15 teach Swift, with a checkpoint exercise every other day

Practise

  • Dart, TypeScript, Kotlin and Swift tracks with small exercises and free mentor feedback

Keep handy

Also worth your time

Mark this stage as not done?