Errors with throw and do catch
Functions that throw are marked throws and called with try. Handle errors with do catch.
Example
enum AgeError: Error { case negative }
func check(_ a: Int) throws -> Int { if a < 0 { throw AgeError.negative }; return a }
do { print(try check(5)); print(try check(-1)) }
catch { print("error:", error) }Exercise
Mark a throwing function.
func f() {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.