Introduction
Swift provides first-class support for handling recoverable runtime errors. Instead of returning special sentinel values or crashing, functions that can fail are marked with the throws keyword and describe their failure using types that conform to the Error protocol. This lets the compiler enforce that callers acknowledge and handle possible failures, making error paths explicit and easy to trace through code.
Cricket analogy: A run-chase calculator marked as able to 'fail' when the required run rate becomes impossible is like a function marked throws, forcing the scorer to explicitly handle the 'match lost' case rather than silently returning a wrong number.
Syntax
enum ValidationError: Error {
case tooShort
case containsInvalidCharacters
}
func validate(username: String) throws -> Bool {
if username.count < 3 {
throw ValidationError.tooShort
}
return true
}
do {
try validate(username: "ab")
} catch {
print("Validation failed: \(error)")
}Explanation
ValidationError is an enum conforming to Error, which is the typical way to model a closed set of failure cases. The validate function is marked throws, signaling it may fail instead of returning normally. Inside, throw immediately exits the function and hands the error to the calling context. A do block wraps any try expressions; if a thrown error propagates out, execution jumps to the matching catch block, where the error is available as the implicit error constant unless you bind it to a named pattern.
Cricket analogy: A NoBallError enum conforming to Error models illegal deliveries; a checkDelivery function marked throws calls throw NoBallError.overstepping the instant the bowler oversteps, and the umpire's do/catch block catches it via the implicit error constant to signal a free hit.
Example
enum NetworkError: Error {
case badURL
case timeout
}
func fetch(urlString: String) throws -> String {
guard urlString.hasPrefix("https://") else {
throw NetworkError.badURL
}
return "data from \(urlString)"
}
// try? converts the result into an Optional
let result = try? fetch(urlString: "ftp://example.com")
print(result ?? "no data")
// try! force-unwraps, crashing on failure
let forced = try! fetch(urlString: "https://example.com")
print(forced)
do {
let value = try fetch(urlString: "bad")
print(value)
} catch NetworkError.badURL {
print("The URL was malformed")
} catch {
print("Unexpected error: \(error)")
}Output
no data
data from https://example.com
The URL was malformedKey Takeaways
- Errors are values conforming to the Error protocol, usually modeled as enums with associated cases.
- Functions that can fail are marked throws and must be called with try inside a do/catch, or with try?/try!.
- do/catch blocks can pattern-match specific error cases before falling back to a generic catch.
- try? converts a throwing call into an Optional, returning nil on failure instead of propagating the error.
- try! force-tries a call and crashes the program at runtime if it throws, so use it only when failure is truly impossible.
- Explicit error handling makes failure paths visible in code and enforced by the compiler.
Practice what you learned
1. What protocol must a type conform to in order to be thrown as an error in Swift?
2. What keyword must precede a call to a throwing function?
3. What does try? do when the underlying call throws an error?
4. What happens when a try! call throws an error at runtime?
5. How are custom error types most commonly represented in Swift?
Was this page helpful?
You May Also Like
The guard Statement in Swift
The guard statement performs early-exit validation, unwrapping optionals for use in the rest of the enclosing scope.
Optionals in Swift
Optionals let a Swift variable represent either a value or the absence of one, forming the basis of Swift's null-safety.
Enumerations in Swift
Learn how Swift enums define related values, support associated and raw values, and integrate with switch statements.
Functions in Swift
Learn how to declare, call, and label the parameters of reusable blocks of code called functions in Swift.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics