Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark the function as throwable.
iOS Swift
func fetchData() [1] {
// code that might throw an error
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throw' instead of 'throws' to mark the function.
Confusing 'try' with the function declaration.
✗ Incorrect
The throws keyword marks a function that can throw an error.
2fill in blank
mediumComplete the code to call a throwable function correctly.
iOS Swift
do {
try [1]()
} catch {
print("Error occurred")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function without 'try' when it can throw.
Using a wrong function name.
✗ Incorrect
Use try before calling a function that can throw an error, like fetchData().
3fill in blank
hardFix the error in the catch block to print the error description.
iOS Swift
do {
try fetchData()
} catch [1] {
print(error.localizedDescription)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'let' before error in catch.
Using an undefined variable name.
✗ Incorrect
The catch block must declare the error with let error to access it.
4fill in blank
hardFill both blanks to throw a custom error inside the function.
iOS Swift
enum DataError: Error {
case [1]
}
func fetchData() throws {
throw DataError.[2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different case names in enum and throw statement.
Forgetting to mark the function as 'throws'.
✗ Incorrect
Define a custom error case notFound and throw it inside the function.
5fill in blank
hardFill both blanks to handle a specific error case in catch.
iOS Swift
do {
try fetchData()
} catch DataError.[1] {
print("Data not found")
} catch [2] {
print("Other error: \(error)")
} catch {
print("Unknown error")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not matching the enum case name exactly.
Not declaring the error variable in catch.
✗ Incorrect
Catch the specific notFound error, then catch other errors by declaring let error and printing it.