somu
(somu)
1
Hi,
Problem:
I have the following function which shows a warning No calls to throwing functions occur within 'try' expression
Questions:
- Why is this warning shown? (The code inside the task throws an error)
- What should I do to propagate the error to the caller of
f1?
Code:
func f1() async throws {
try await withThrowingTaskGroup(of: Int.self) { group in //No calls to throwing functions occur within 'try' expression
group.addTask(priority: .high) {
throw NSError()
}
}
}
No, the code inside the closure passed to group.addTask throws. The closure passed to withThrowingTaskGroup does not throw, so calling withThrowingTaskGroup with the try keyword produces a warning.
2 Likes
somu
(somu)
3
Thanks @Peter-Schorn so is calling try await group.waitForAll() as shown below the right way to escalate the error to f1?
func f1() async throws {
try await withThrowingTaskGroup(of: Int.self) { group in
group.addTask(priority: .high) {
throw NSError(domain: "testing error", code: 21, userInfo: nil)
}
try await group.waitForAll()
}
}
Yes, that will cause the error to be thrown to the caller of f1