Basics: Error Handling

You use error handling to respond to error conditions your program may encounter during execution.

Allows you to determine the underlying cause of failure, and, if necessary, propagate the error to another part of your program.

Func canThrowAnError() throws {
    // this function may or may not throw an error
}
Do {
    try canThrowAnError()
    // no error was thrown
} catch {
    // an error was thrown
}
Func makeASandwich() throws {
    // ...
}

do {
    try makeASandwich()
    eatASandwich()
} catch SandwichError.outOfCleanDishes {
    washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
    buyGroceries(ingredients)
}

Basics: Error Handling — Structure map

Clickable & Draggable!

Basics: Error Handling — Related pages: