Recall & Review
beginner
What does the
throws keyword mean in a Swift function?It means the function can produce an error that must be handled by the caller using
try, try?, or try!.Click to reveal answer
beginner
How do you call a function that can throw an error?
You use the
try keyword before the function call, and handle errors with do-catch or propagate them further.Click to reveal answer
beginner
What is the purpose of the
do-catch block in Swift?It lets you run code that might throw errors and catch those errors to handle them safely.
Click to reveal answer
beginner
What happens if you call a throwing function without
try?The code will not compile because Swift requires explicit handling of possible errors.
Click to reveal answer
intermediate
Explain the difference between
try? and try!.try? converts the error into an optional value (nil if error), while try! forces the call and crashes if an error occurs.Click to reveal answer
What keyword must a Swift function include if it can throw an error?
✗ Incorrect
The
throws keyword marks a function that can throw errors.How do you handle errors when calling a throwing function?
✗ Incorrect
You must use
try and handle errors with do-catch or propagate them.What does
try? do when a throwing function fails?✗ Incorrect
try? returns an optional nil if an error occurs.What happens if you call a throwing function without
try?✗ Incorrect
Swift requires
try to call throwing functions; otherwise, it won't compile.Which block is used to catch errors thrown by a function?
✗ Incorrect
Swift uses
do-catch blocks to catch and handle errors.Describe how to write and call a throwing function in Swift.
Think about marking the function and how the caller must handle errors.
You got /4 concepts.
Explain the difference between
try, try?, and try! when calling throwing functions.Consider how each handles errors differently.
You got /3 concepts.