Complete the code to declare a function that can throw an error.
func checkNumber(_ number: Int) [1] { if number < 0 { throw NSError(domain: "Negative number", code: 1) } }
The keyword throws is used to declare that a function can throw an error.
Complete the code to call a throwing function using the correct keyword.
do {
try [1](5)
} catch {
print("Error occurred")
}When calling a throwing function, you use the try keyword followed by the function name.
Fix the error in the function declaration to properly indicate it can throw errors.
func validateAge(_ age: Int) [1] { if age < 18 { throw NSError(domain: "Too young", code: 2) } }
The function declaration must use throws to indicate it can throw errors, not throw.
Fill both blanks to create a throwing function and call it correctly.
func [1](_ value: Int) [2] { if value == 0 { throw NSError(domain: "Zero value", code: 3) } }
The function name is checkValue and it must be declared with throws to indicate it can throw errors.
Fill all three blanks to declare a throwing function, call it with try, and handle errors.
func [1](_ input: String) [2] { if input.isEmpty { throw NSError(domain: "Empty input", code: 4) } } do { [3] [1]("") } catch { print("Caught an error") }
The function name is processInput, declared with throws. When calling it, use try to handle possible errors.