Activity 1
Create validation errors
Define errors for an empty title and a title that is too long. Write a throwing validator and handle both cases.
Hint: Associated values can include useful context such as the maximum length.
Swift Fundamentals
Represent recoverable failures with Error, throw, throws, do-catch, and defer.
Before you begin
Learning stage
Swift error handling represents recoverable failures that a caller may need to understand. Any type conforming to Error can be thrown; enums work well when failure cases form a finite set.
A throwing function marks its contract with throws. Callers use try and can handle failures with do-catch, propagate them from another throwing function, convert them to optionals with try?, or deliberately assert with try! only when failure is impossible by design.
Learning stage
The validator throws distinct domain errors and the caller handles each one explicitly.
enum EnrollmentError: Error {
case emptyName
case courseFull
}
func enroll(name: String, availableSeats: Int) throws -> String {
guard !name.isEmpty else { throw EnrollmentError.emptyName }
guard availableSeats > 0 else { throw EnrollmentError.courseFull }
return "Enrolled \(name)"
}
do {
print(try enroll(name: "Mina", availableSeats: 1))
} catch EnrollmentError.emptyName {
print("Enter your name")
} catch EnrollmentError.courseFull {
print("The course is full")
} catch {
print("Unexpected error: \(error)")
}Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Define errors for an empty title and a title that is too long. Write a throwing validator and handle both cases.
Hint: Associated values can include useful context such as the maximum length.
Activity 2
For a dictionary lookup, a network request, and optional profile biography, decide whether Optional or throws communicates each result better.
Hint: Ask whether absence is expected or whether callers need a failure reason.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
Error
Any type conforming to Error can participate in Swift's throwing system.
QUESTION 2
It converts the result to an optional and produces nil for the failure.
Use it only when losing the specific error is acceptable.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Use an optional for expected absence without needed detail; throw when failure reasons or recovery decisions matter.
An optional communicates value-or-no-value. A throwing API can communicate distinct failure cases and propagate them. The choice is part of the contract: callers should receive only the information they can use meaningfully.
Possible follow-ups
Learning stage
Errors communicate recoverable failure with context. Design failure contracts deliberately, preserve useful information, and handle errors at the layer that can make a meaningful decision.