Swift 6 · Errors and concurrency
Throw and catch errors
Mark functions that can fail with throws, call them with try, and handle errors in do/catch.
enum ValidationError: Error { case empty }
func validate(_ text: String) throws {
if text.isEmpty { throw ValidationError.empty }
}
do {
try validate("")
} catch {
print("failed: \(error)")
}
Official Swift 6 documentation