0Pricing
Swift Academy · Lesson

do/catch & defining errors (enums)

Define error enums, throw them from functions, and handle failures with do/catch patterns (specific cases, catch let, generic catch).

Overview

This lesson focuses on defining typed errors and handling them with do/catch. You'll throw an enum and catch specific cases for clear recovery paths.

Error enum

Define a custom enum that conforms to Error. Associated values add helpful context.

enum FileError: Error {
    case notFound(path: String)
    case unreadable(path: String)
    case corrupted
}
print("Defined FileError enum")

All lessons in this course

  1. Optional chaining & error handling basics
  2. do/catch & defining errors (enums)
  3. Result types — Result
← Back to Swift Academy