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?
Ado
Bcatch
Cthrow
Dtry
✗ Incorrect
You use try before calling a function that can throw an error.
What happens if an error is thrown inside a do block?
AExecution jumps to the matching <code>catch</code> block.
BThe error is ignored.
CThe program continues normally.
DThe program crashes immediately.
✗ 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?
Atry
Bcatch
Cthrow
Ddo
✗ Incorrect
The catch block handles errors thrown in the do block.
What does try? do in Swift?
AForces the function to throw an error.
BIgnores the error completely.
CConverts the error to an optional value.
DCatches the error automatically.
✗ Incorrect
try? converts a thrown error into an optional nil value.
Can you have multiple catch blocks in Swift?
AYes, to handle different error types.
BNo, errors are handled in the do block.
CYes, but only one will run.
DNo, only one catch block is allowed.
✗ 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.
Practice
(1/5)
1. What is the main purpose of using do-try-catch in Swift?
easy
A. To handle errors safely without crashing the program
B. To create loops that run multiple times
C. To define new functions
D. To declare variables with default values
Solution
Step 1: Understand the role of do-try-catch
This structure is used to run code that might throw errors and handle those errors gracefully.
Step 2: Identify the purpose
It prevents the program from crashing by catching errors and allowing alternative actions.
Final Answer:
To handle errors safely without crashing the program -> Option A
Quick Check:
Error handling = A [OK]
Hint: Remember: try means risky code, catch means safe backup [OK]
Common Mistakes:
Confusing error handling with loops
Thinking try-catch defines functions
Assuming it declares variables
2. Which of the following is the correct syntax to start a do-try-catch block in Swift?
easy
A. try someFunction() do { catch handleError() }
B. try { do someFunction() } catch { handleError() }
C. catch { try someFunction() } do { handleError() }
D. do { try someFunction() } catch { handleError() }
Solution
Step 1: Recall correct do-try-catch syntax
The correct syntax starts with do {}, inside which try is used before the function that might throw.
Step 2: Match syntax with options
do { try someFunction() } catch { handleError() } correctly places do block, uses try inside, and follows with catch block.
Final Answer:
do { try someFunction() } catch { handleError() } -> Option D
Quick Check:
Correct syntax = D [OK]
Hint: Remember: do { try ... } catch { ... } is the pattern [OK]
Common Mistakes:
Placing try outside do block
Swapping catch and do blocks
Missing curly braces
3. What will be the output of the following Swift code?
enum MyError: Error { case fail }
func risky() throws {
throw MyError.fail
}
do {
try risky()
print("Success")
} catch {
print("Caught an error")
}
medium
A. Success
B. Caught an error
C. No output
D. Compile-time error
Solution
Step 1: Analyze the function risky()
This function always throws an error of type MyError.fail.
Step 2: Trace the do-try-catch block
When try risky() runs, it throws an error, so the code jumps to the catch block and prints "Caught an error".
Final Answer:
Caught an error -> Option B
Quick Check:
Error thrown = catch block runs = B [OK]
Hint: If try throws, catch block runs, not code after try [OK]
The first catch handles FileError.notFound, which does not match. The second catch is a general catch that handles all other errors, so it runs and prints "Other error".
Final Answer:
Other error -> Option C
Quick Check:
Specific catch misses, general catch runs = A [OK]
Hint: Specific catch runs only if error matches exactly [OK]
Common Mistakes:
Assuming first catch runs for all errors
Thinking no output occurs
Confusing error types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.