Bird
Raised Fist0
iOS Swiftmobile~5 mins

Error handling (try, catch, throw) in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of try in Swift error handling?
The try keyword is used to call a function that can throw an error. It tells Swift to watch for errors and handle them if they occur.
Click to reveal answer
beginner
What does catch do in Swift?
catch is used to handle errors thrown by a try statement. It lets you respond to different errors and keep your app running smoothly.
Click to reveal answer
beginner
How do you throw an error in Swift?
You use the throw keyword followed by an error value to signal that something went wrong. This passes the error up to be handled by try and catch.
Click to reveal answer
intermediate
What is a do-catch block in Swift?
A do-catch block lets you try code that might throw errors inside the do section, and handle any errors in the catch section.
Click to reveal answer
intermediate
What happens if you call a throwing function without try?
Swift will give a compile-time error because it requires you to handle possible errors explicitly using try, try?, or try!.
Click to reveal answer
Which keyword do you use to call a function that might throw an error in Swift?
Acatch
Btry
Cthrow
Ddo
What keyword is used to handle errors after a try statement?
Acatch
Bthrow
Cdo
Dtry
How do you signal an error inside a function in Swift?
Atry
Bcatch
Cthrow
Ddo
What is the purpose of a do-catch block?
ATo declare a throwing function
BTo ignore errors
CTo throw errors
DTo handle errors from throwing functions
What happens if you call a throwing function without using try?
ASwift gives a compile-time error
BSwift throws a runtime error
CThe function runs normally
DThe error is ignored
Explain how Swift uses try, catch, and throw to handle errors.
Think about how you ask for help when something goes wrong.
You got /3 concepts.
    Describe a simple example of a function that throws an error and how you would call it safely.
    Imagine a vending machine that can run out of snacks.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the try keyword do in Swift error handling?
      easy
      A. It catches an error thrown by a function.
      B. It calls a function that might throw an error and requires handling.
      C. It defines a new error type.
      D. It throws an error manually.

      Solution

      1. Step 1: Understand the role of try

        The try keyword is used before calling a function that can throw an error, signaling that error handling is needed.
      2. Step 2: Differentiate from other keywords

        catch handles errors, throw raises errors, and defining errors uses enum or struct.
      3. Final Answer:

        It calls a function that might throw an error and requires handling. -> Option B
      4. Quick Check:

        try calls throwing functions [OK]
      Hint: Remember: try calls, catch handles, throw creates errors [OK]
      Common Mistakes:
      • Confusing try with catch or throw keywords
      • Thinking try itself catches errors
      • Using try without a throwing function
      2. Which of the following is the correct syntax to catch an error in Swift?
      easy
      A. do { try someFunction() } catch { print("Error") }
      B. try { someFunction() } catch { print("Error") }
      C. catch { try someFunction() }
      D. do catch { try someFunction() }

      Solution

      1. Step 1: Recall the correct do-catch syntax

        In Swift, error handling uses do { try ... } catch { ... } blocks to catch errors.
      2. Step 2: Identify the correct option

        do { try someFunction() } catch { print("Error") } correctly wraps the try call inside a do block followed by a catch block.
      3. Final Answer:

        do { try someFunction() } catch { print("Error") } -> Option A
      4. Quick Check:

        do-catch syntax = do { try someFunction() } catch { print("Error") } [OK]
      Hint: Remember: do { try } catch { } is the pattern [OK]
      Common Mistakes:
      • Placing try outside do block
      • Using catch without do
      • Incorrect order of try and catch
      3. What will be printed when running this Swift code?
      enum FileError: Error { case notFound }
      func readFile() throws {
        throw FileError.notFound
      }
      do {
        try readFile()
        print("File read successfully")
      } catch {
        print("Failed to read file")
      }
      medium
      A. Compile error
      B. File read successfully
      C. No output
      D. Failed to read file

      Solution

      1. Step 1: Analyze the throwing function

        The function readFile() always throws FileError.notFound, so it never completes normally.
      2. Step 2: Understand the do-catch behavior

        The try readFile() call throws an error, so control jumps to the catch block, printing "Failed to read file".
      3. Final Answer:

        Failed to read file -> Option D
      4. Quick Check:

        Thrown error triggers catch block [OK]
      Hint: Thrown error skips try block print, runs catch print [OK]
      Common Mistakes:
      • Assuming code after try runs if error thrown
      • Thinking no output if error thrown
      • Confusing error throwing with returning
      4. Identify the error in this Swift code snippet:
      func riskyFunction() throws {
        throw NSError(domain: "Error", code: 1)
      }
      try riskyFunction()
      medium
      A. Missing do-catch block to handle the thrown error.
      B. The function cannot throw NSError type errors.
      C. The throw statement syntax is incorrect.
      D. try keyword is not allowed with throwing functions.

      Solution

      1. Step 1: Check error handling usage

        The code calls a throwing function with try but does not handle errors with do-catch or propagate with try? or try!.
      2. Step 2: Confirm syntax correctness

        The throw statement and function declaration are correct; the problem is missing error handling around the call.
      3. Final Answer:

        Missing do-catch block to handle the thrown error. -> Option A
      4. Quick Check:

        Throwing calls need error handling [OK]
      Hint: Always handle errors from throwing functions with do-catch or try? [OK]
      Common Mistakes:
      • Calling throwing functions without error handling
      • Confusing throw syntax with try syntax
      • Assuming try alone handles errors
      5. You want to write a function loadData() that throws a custom error DataError.invalidFormat if data is bad. Which code correctly defines the error and throws it inside the function?
      hard
      A. class DataError: Error { case invalidFormat } func loadData() throws { throw DataError.invalidFormat }
      B. struct DataError { case invalidFormat } func loadData() throws { throw DataError.invalidFormat }
      C. enum DataError: Error { case invalidFormat } func loadData() throws { throw DataError.invalidFormat }
      D. enum DataError { case invalidFormat } func loadData() throws { throw DataError.invalidFormat }

      Solution

      1. Step 1: Define a custom error type conforming to Error

        In Swift, custom errors are usually enums conforming to the Error protocol, like enum DataError: Error.
      2. Step 2: Throw the custom error inside the function

        The function loadData() declares throws and throws DataError.invalidFormat correctly.
      3. Final Answer:

        enum DataError: Error { case invalidFormat } func loadData() throws { throw DataError.invalidFormat } -> Option C
      4. Quick Check:

        Custom errors use enum + Error protocol [OK]
      Hint: Use enum conforming to Error for custom errors [OK]
      Common Mistakes:
      • Not conforming error type to Error protocol
      • Using struct or class without Error conformance
      • Forgetting throws keyword in function
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard