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 — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “try await and error propagation” lesson free?
Yes — the full text of “try await and error propagation” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
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