April 2026
Intermediate
683 pages
25h 29m
English
Throwing errors is all good and fine, but if the client code isn’t catching and handling them properly, then the work is only half done. In this section, we’ll focus on that second half, completing the groundwork for solid apps.
As a framework for the error-catching syntax, Listing 12.10 reintroduces an age-validation example. This time, we are validating the age entered by the user to make it a bit more realistic.
enum AgeValidationError: Error { case negative case tooYoung(minimum: Int)}func validateAge(_ age: Int) throws { if age < 0 { throw AgeValidationError.negative } if age < 18 { throw AgeValidationError.tooYoung(minimum: 18) }}print("Enter your age: ", terminator: "")let userAge ...
Read now
Unlock full access