Recall & Review
beginner
What is the purpose of the
do block in Swift's do-try-catch execution flow?The
do block contains code that might throw an error. It allows you to try running this code and catch errors if they happen.Click to reveal answer
beginner
What keyword do you use to call a function that can throw an error inside a
do block?You use the
try keyword before the function call to indicate it might throw an error.Click to reveal answer
intermediate
What happens when an error is thrown inside a
do block?The program immediately jumps to the matching
catch block to handle the error. The rest of the do block is skipped.Click to reveal answer
intermediate
Can you have multiple
catch blocks in Swift? What is their use?Yes, you can have multiple
catch blocks to handle different types of errors separately, making error handling more precise.Click to reveal answer
advanced
What is the role of the
try? and try! keywords in Swift?try? converts errors to optional values (nil if error), allowing safe error ignoring. try! forces the call and crashes if an error occurs, so use only when sure no error will happen.Click to reveal answer
What keyword do you use to handle errors thrown by a function in Swift?
✗ Incorrect
You use
try before calling a function that can throw an error.What happens if an error is thrown inside a
do block?✗ Incorrect
When an error is thrown, Swift jumps to the
catch block to handle it.Which keyword is used to define the block that catches errors?
✗ Incorrect
The
catch block handles errors thrown in the do block.What does
try? do in Swift?✗ Incorrect
try? converts a thrown error into an optional nil value.Can you have multiple
catch blocks in Swift?✗ Incorrect
Multiple
catch blocks allow handling different errors separately.Explain the flow of execution in a Swift do-try-catch block when an error is thrown.
Think about what happens step-by-step when an error occurs.
You got /5 concepts.
Describe the difference between try, try?, and try! in Swift error handling.
Focus on how each keyword treats errors differently.
You got /3 concepts.