Explain it simply
Error Handling, in plain language
Any app can run into runtime issues — a missing file, a failed network request, a failed payment, bad input. Swift lets you handle errors properly with throw , throws , do-catch , try .
Problem → Why → Solution
The problem — why this concept matters
Calling a function that needs try without wrapping it in do-catch can cause a compile error.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
For Error Handling, define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.
- optional or fallible input
- pattern and error handling
- safe value or recovery
Explain the type, ownership, and runtime behavior of Error Handling
Core syntax or pattern
enum LoginError: Error {
case emptyUsername
case wrongPassword
}
func login(username: String, password: String) throws {
if username.isEmpty {
throw LoginError.emptyUsername
}
if password != "123456" {
throw LoginError.wrongPassword
}
print("Login success")
}
do {
try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
print("Username is required")
} catch LoginError.wrongPassword {
print("Password is incorrect")
} catch {
print("Something went wrong")
}For Error Handling, define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.
Read the code step by step
- optional or fallible input
enum LoginError: Error {Any app can run into runtime issues — a missing file, a failed network request, a failed payment, bad input. Swift lets you handle errors properly with throw , throws , do-catch , try .
- pattern and error handling
print("Login success")For Error Handling, define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.
- safe value or recovery
}Run and test an original Error Handling example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- optional or fallible input
- pattern and error handling
- safe value or recovery
Success
Explain the type, ownership, and runtime behavior of Error Handling
Failure
Calling a function that needs try without wrapping it in do-catch can cause a compile error.
Any app can run into runtime issues — a missing file, a failed network request, a failed payment, bad input. Swift lets you handle errors properly with throw, throws, do-catch, try.
enum LoginError: Error {
case emptyUsername
case wrongPassword
}
func login(username: String, password: String) throws {
if username.isEmpty {
throw LoginError.emptyUsername
}
if password != "123456" {
throw LoginError.wrongPassword
}
print("Login success")
}
do {
try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
print("Username is required")
} catch LoginError.wrongPassword {
print("Password is incorrect")
} catch {
print("Something went wrong")
}login function is marked throws since it can fail. Calling it requires try, and if it fails you land in one of the catch blocks.
Password is incorrectReal-world usage
How to apply it in a real project
Run and test an original Error Handling example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Error Handling example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
enum LoginError: Error {
case emptyUsername
case wrongPassword
}
func login(username: String, password: String) throws {
if username.isEmpty {
throw LoginError.emptyUsername
}
if password != "123456" {
throw LoginError.wrongPassword
}
print("Login success")
}
do {
try login(username: "sai", password: "wrong")
} catch LoginError.emptyUsername {
print("Username is required")
} catch LoginError.wrongPassword {
print("Password is incorrect")
} catch {
print("Something went wrong")
}Expected: Explain the type, ownership, and runtime behavior of Error Handling
Show hint
Run and test an original Error Handling example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- optional or fallible input → pattern and error handling → safe value or recovery
- For Error Handling, define the input domain, exhaustive paths, return values, side effects, and failure or cancellation behavior. Prefer guard statements, typed errors, and explicit propagation over force unwraps or silent suppression.
- Calling a function that needs try without wrapping it in do-catch can cause a compile error.
- Run and test an original Error Handling example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org