Learn to handle runtime issues effectively with Swift’s error-handling techniques. This chapter covers error types, throwing and catching errors, the do-catch statement, and error propagation with throws.
Topic 1: Error Types in Swift
What protocol must a type conform to in order to represent an error in Swift? a) ErrorRepresentable b) CustomStringConvertible c) Error d) NSError
Which of the following best describes an error in Swift? a) A runtime exception b) A value of a type conforming to Error c) A logic failure d) None of the above
What is a common way to define custom error types in Swift? a) Using struct b) Using enum c) Using class d) All of the above
How can you associate additional information with a custom error type? a) By adding associated values in an enum b) By defining properties in a struct c) By defining properties in a class d) All of the above
Which built-in Swift error type is typically used in Foundation-based APIs? a) Error b) NSError c) CustomError d) None of the above
Topic 2: Throwing and Catching Errors
How do you indicate that a function can throw an error? a) By using throws in the function signature b) By using throwable in the function signature c) By using error in the function body d) By using catch in the function signature
Which keyword is used to throw an error in Swift? a) error b) raise c) throw d) catch
What type of value can be thrown as an error in Swift? a) Any type b) Only instances of Error c) Only instances of NSError d) Only String values
What happens if an error is thrown inside a function that doesn’t declare throws? a) The program crashes b) A compile-time error occurs c) The error is ignored d) It gets automatically propagated
Can a throw statement return a value in Swift? a) Yes, always b) No, never c) Yes, if it’s a string d) None of the above
Topic 3: The do-catch Statement
What is the purpose of a do block in Swift? a) To define an error type b) To test a block of code for errors c) To propagate an error d) None of the above
Which of the following is correct syntax for a do-catch block? a) do { try function() } catch { print(error) } b)do { function() } onError { print(error) } c)try { function() } catch { print(error) } d) None of the above
What happens if no catch block matches the thrown error? a) The error is ignored b) The program crashes c) The error propagates further d) None of the above
Can a do block contain multiple catch blocks? a) Yes b) No c) Only for specific error types d) None of the above
How can you catch only specific error types in a do-catch block? a) By using if let b) By using case statements in catch c) By specifying error codes d) All of the above
Topic 4: Error Propagation with throws
How do you indicate that a function propagates errors? a) Add throws to the function signature b) Add error to the function body c) Use catch in the function implementation d) None of the above
What happens to an error that propagates out of a throws function? a) It must be handled by the caller b) It is automatically ignored c) It causes a crash d) It is converted into an exception
Can an initializer in Swift throw an error? a) Yes, by adding throws to the initializer b) No, initializers cannot throw errors c) Only in subclasses d) None of the above
How can you call a throws function without handling the error directly? a) Using try! b) Using try? c) Both a and b d) None of the above
Which of the following is correct for error propagation? a)func example() throws { try anotherFunction() } b) func example() { try anotherFunction() } c) func example() error { anotherFunction() } d) None of the above