try await and error propagation
Use try await with throwing async functions, handle failures with do/catch , and propagate errors up cleanly (including CancellationError ).
try await and error propagation is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
Why try await?
try await calls a throwing async function. If it throws, the error propagates unless you catch it. You can also use try? to convert to Optional.
Throwing async + do/catch
Create a throwing async function and call it with try await inside do/catch to handle failures.
enum NetError: Error { case offline; case badStatus(Int) }
func fetchNumber(from ok: Bool) async throws -> Int {
try await Task.sleep(nanoseconds: 100_000_000)
if !ok { throw NetError.offline }
return 7
}
Task {
do {
let n = try await fetchNumber(from: true)
print("value:", n) // 7
} catch {
print("failed:", error) // handled here
}
}Converting errors
Use try? to get an Optional instead of throwing. Avoid try! unless you are certain it cannot fail.
Task {
// try? converts failure to nil
let maybe = try? await fetchNumber(from: false)
print("optional result:", String(describing: maybe)) // nil on error
// ⚠️ try! will crash if an error is thrown — avoid in production
// let forced = try! await fetchNumber(from: false)
}Bubble up errors
Mark callers as async throws to propagate errors upward; they can be handled at a higher boundary.
func loadProfile(ok: Bool) async throws -> String {
let id = try await fetchNumber(from: ok) // propagate on error
return "id:\\(id)"
}
Task {
do {
let s = try await loadProfile(ok: true)
print(s) // "id:7"
} catch {
print("propagated:", error)
}
}Specific catches
Use pattern-matching catch blocks. Handle CancellationError first to keep shutdown paths fast and quiet.
enum FileError: Error { case notFound }
func mightCancel() async throws {
for _ in 0..<5 {
try Task.checkCancellation() // throws CancellationError
try await Task.sleep(nanoseconds: 30_000_000)
}
}
Task {
let t = Task { try await mightCancel() }
t.cancel()
do {
try await t.value
} catch is CancellationError {
print("cancelled cleanly") // handle cancellation separately
} catch {
print("other error:", error)
}
}Guidelines
Tips:
- Propagate with try until a boundary that can decide (UI, CLI, or top-level Task).
- Prefer domain errors (enums) for clarity.
- Convert with try? only when absence is acceptable.
- Catch CancellationError distinctly.
Error handling with try await
Quick check: What does try await f() do with errors?
Recap
Recap: Use try await with throwing async functions, bubble errors up from helpers, convert with try? when optional is fine, and catch CancellationError explicitly.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “try await and error propagation” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “try await and error propagation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
What will I learn in “try await and error propagation”?
Use try await with throwing async functions, handle failures with do/catch , and propagate errors up cleanly (including CancellationError ). You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
How long does the “try await and error propagation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Structured concurrency, async functions
- async let, Task, cancellation
- try await and error propagation