0
0
iOS Swiftmobile~10 mins

Error handling (try, catch, throw) in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acatch
Bthrows
Cthrow
Dtry
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throw' instead of 'throws' to mark the function.
Confusing 'try' with the function declaration.
2fill in blank
medium

Complete 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'
AfetchData
BcatchError
CthrowError
DhandleError
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function without 'try' when it can throw.
Using a wrong function name.
3fill in blank
hard

Fix 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'
Alet error
Berror
CcatchError
Dvar error
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'let' before error in catch.
Using an undefined variable name.
4fill in blank
hard

Fill 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'
AnotFound
BinvalidData
Dtimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Using different case names in enum and throw statement.
Forgetting to mark the function as 'throws'.
5fill in blank
hard

Fill 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'
AnotFound
Blet error
Cerror
DDataError
Attempts:
3 left
💡 Hint
Common Mistakes
Not matching the enum case name exactly.
Not declaring the error variable in catch.