问题:
我有以下函数,它显示警告信息在'try'表达式内没有调用抛出异常的函数
- 为什么会出现这个警告?(任务中的代码引发了错误)
- 我该如何将错误传播到
f1
的调用方?
代码:
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()
}
}
}
group.waitForAll()
,withThrowingTaskGroup
是否会等待所有任务完成? - user1046037withThrowingTaskGroup
返回一个抛出操作。但是在这里你没有这样做,因此会收到警告。如果你不打算等待组中的任何值,则waitForAll
只是消除警告的方法。 - Sweeper