Closed3
Taskはエラーを握りつぶす?
ふんわりコードを見ていてふとtryの挙動に引っかかった
func throwableFunction() throws -> String? {
// Simulating an error
throw NSError(domain: "com.example.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "An error occurred"])
return "Success"
}
func callThrowableFunction(){
try throwableFunction() // ⚠️Errors thrown from here are not handled
guard let throwable = try throwableFunction() else { return } // ⚠️Errors thrown from here are not handled
guard let throwable = try? throwableFunction() else { return } // 何も言われない
}
func callThrowableFunction(){
Task {
try throwableFunction()
guard let throwable = try throwableFunction() else { return }
guard let throwable = try? throwableFunction() else { return }
}
}
こうすると全て黙る なぜ?
Taskのイニシャライザを見る
@discardableResult
public init(
priority: TaskPriority? = nil,
operation: sending @escaping @isolated(any) () async throws -> Success
)
priorityとoperationが引数に存在する。trailing closureを使って普段Taskで囲って書いている部分はoperation。
そこを見ると確かにthrowsと書いているので、closure内でtryを使っても警告されない。
このスクラップは5ヶ月前にクローズされました