Explore advanced error handling techniques in Swift, including custom error types, recoverable and non-recoverable errors, the Result type, and advanced error propagation strategies. Test your knowledge with 30 multiple-choice questions!
Custom Error Types (Questions 1-10)
What is the keyword used to define a custom error type in Swift? a) custom b) enum c) struct d) error
Which of the following is the correct way to define a custom error type in Swift? a) enum MyError: Error {} b) struct MyError: Error {} c) class MyError: Error {} d) error MyError {}
What protocol must a custom error type conform to in Swift? a) Error b) CustomError c) Failure d) Exception
Which statement about custom error types in Swift is false? a) Custom error types must conform to the Error protocol b) Custom error types can include associated values c) Custom error types can be enum or struct d) Custom error types can only be enums
What is an associated value in a custom error type used for? a) Storing extra information related to the error b) Storing a description of the error c) Specifying the error type d) Formatting error messages
What is the correct way to throw a custom error in Swift? a) throw MyError.invalidInput b) throw "MyError.invalidInput" c) throw new MyError.invalidInput() d) throw new MyError()
Can you use custom error types in Swift with do-catch blocks? a) Yes b) No
How can you make a custom error type more descriptive? a) By adding description property b) By using error codes c) By including associated values d) By adding a default message
What happens when a throw statement is encountered in a function? a) The function stops execution and returns a value b) The function continues executing c) The program crashes d) An exception is raised, and control is passed to a catch block
Which of the following can be an associated value for a custom error type? a) String b) Int c) Bool d) All of the above
Recoverable and Non-Recoverable Errors (Questions 11-16)
What is the primary difference between recoverable and non-recoverable errors? a) Recoverable errors can be handled with do-catch; non-recoverable cannot b) Recoverable errors allow you to retry; non-recoverable do not c) Non-recoverable errors do not generate error messages d) Recoverable errors occur during runtime; non-recoverable happen during compile time
Which type of error should be classified as recoverable? a) File not found b) Network timeout c) Division by zero d) Invalid input format
How can you handle recoverable errors in Swift? a) By using try? b) By using try! c) By using do-catch with retries d) By using fatalError()
Which of the following is a non-recoverable error? a) Out of memory b) Network connection failure c) File permissions error d) Invalid user input
Can non-recoverable errors be handled by a catch block? a) Yes, with appropriate error handling b) No, they cause the program to terminate
What happens if a recoverable error is not caught using do-catch? a) The program continues execution normally b) A warning is shown to the user c) The error is ignored d) The program crashes
Result Type (Result<Success, Failure>) (Questions 17-22)
What does the Result type represent in Swift? a) Success and failure values with associated data b) Only success with a success message c) Failure only with an error message d) A data type that cannot be used with errors
How is the Result type initialized in Swift? a) Result.success(value) b) Result.failure(error) c) Result(success: value, error: error) d) Both a and b
What does Result<Success, Failure> do when using the success case? a) Returns the associated value of type Success b) Returns an error c) Indicates a failed operation d) Returns nothing
Which statement about Result is false? a) Result can hold either a success or failure case b) Result is an enum type c) You cannot define your own success and failure types d) Result can be used to wrap errors and values
How do you access the value from a Result type when it is successful? a) result.success b) result.successValue c) result.get() d) result.value
What is the best use case for using Result? a) When you expect a value and want to handle success/failure explicitly b) For handling exceptions during runtime c) For managing thread execution d) For synchronous data processing
Advanced Error Propagation (Questions 23-30)
What does error propagation allow in Swift? a) Passing errors from one function to another b) Creating custom error types c) Handling errors at the point of failure d) Ignoring errors and continuing execution
Which of the following is a method for propagating errors in Swift? a) try! b) try? c) throw d) All of the above
What happens when you use try! in Swift? a) The program throws an error if an exception occurs b) The function continues execution regardless of errors c) The error is propagated without any handling d) The program crashes if an error occurs
What is the difference between try? and try! in Swift? a) try? returns an optional, while try! forces a runtime error b) try? will always handle errors, while try! ignores them c) try? causes an immediate crash, while try! handles errors d) There is no difference
How can you handle errors from asynchronous functions in Swift? a) By using async try and await b) By using do-catch with a Task c) By using try? inside a closure d) By ignoring errors with ignoreErrors()
What does the throws keyword indicate in a function declaration? a) The function may throw an error during execution b) The function must throw an error c) The function handles errors internally d) The function is guaranteed to succeed
What type of error handling is recommended when dealing with multiple error conditions in a function? a) Nested do-catch blocks b) Using Result type for better clarity c) Returning optional values d) Throwing runtime errors
When is it appropriate to use try? for error propagation? a) When you expect an error but want to handle it as nil b) When you expect a success and don’t care about errors c) When you want to crash the program on errors d) When the error is non-recoverable
Answer Key
QNo
Answer (Option with Text)
1
b) enum
2
a) enum MyError: Error {}
3
a) Error
4
d) Custom error types can only be enums
5
a) Storing extra information related to the error
6
a) throw MyError.invalidInput
7
a) Yes
8
c) By including associated values
9
a) The function stops execution and returns a value
10
d) All of the above
11
b) Recoverable errors allow you to retry; non-recoverable do not
12
b) Network timeout
13
c) By using do-catch with retries
14
a) Out of memory
15
b) No, they cause the program to terminate
16
a) The program continues execution normally
17
a) Success and failure values with associated data
18
d) Both a and b
19
a) Returns the associated value of type Success
20
c) You cannot define your own success and failure types
21
c) result.get()
22
a) When you expect a value and want to handle success/failure explicitly
23
a) Passing errors from one function to another
24
d) All of the above
25
d) The program crashes if an error occurs
26
a) try? returns an optional, while try! forces a runtime error
27
b) By using do-catch with a Task
28
a) The function may throw an error during execution
29
b) Using Result type for better clarity
30
a) When you expect an error but want to handle it as nil