Swift Error Handling: Try, Catch u0026 Throw Fundamentals Quiz Quiz

Explore and reinforce your understanding of Swift error handling, focusing on try, catch, throw, and related keywords. This quiz covers practical scenarios and key concepts essential for beginners learning to manage errors effectively in Swift programming.

  1. Understanding 'throw' keyword usage

    Which keyword is used in Swift to indicate that an error has occurred within a function or method?

    1. try
    2. throw
    3. throws
    4. catch

    Explanation: The 'throw' keyword is used within a function or method to signal that an error has occurred. 'throws' is used in the function declaration to indicate that the function can throw errors, but not to actually throw one. 'catch' is used for handling errors, and 'try' is used when calling a throwing function. Only 'throw' can actually signal an error occurrence.

  2. Function declaration for error throwing

    What annotation must be added to a Swift function's declaration to allow it to throw errors?

    1. throw
    2. catch
    3. throws
    4. error

    Explanation: 'throws' is placed after the parameter list in a function declaration to allow the function to throw errors. The word 'throw' is used inside the function, not in its declaration. 'catch' and 'error' are not valid annotations for this purpose. Passing 'throws' signals to the caller that the function may produce an error.

  3. Purpose of the 'try' keyword

    When calling a function that can throw an error in Swift, what keyword must be used before the function call?

    1. throwing
    2. try
    3. catch
    4. throw

    Explanation: The 'try' keyword is required before calling a function that can throw an error, indicating the operation might fail and needs proper handling. 'throw' is for generating errors, not calling. 'catch' is for error handling after a thrown error, and 'throwing' is not a valid Swift keyword. Only 'try' initiates the throwing call.

  4. Catching errors with 'do-catch'

    Which code structure is used in Swift to handle errors thrown by functions that use 'throw'?

    1. guard-else
    2. repeat-while
    3. do-catch
    4. if-else

    Explanation: Swift uses 'do-catch' blocks to catch and handle errors thrown by functions. 'if-else' and 'guard-else' are general control flow statements but do not handle thrown errors. 'repeat-while' is used for looping and is unrelated to error handling. Only 'do-catch' is tailored for this purpose.

  5. Understanding 'try?' usage

    What does using the 'try?' keyword before a throwing function return if an error is thrown?

    1. nil
    2. false
    3. 0
    4. Throws an uncaught error

    Explanation: Using 'try?' attempts to call a throwing function and automatically returns nil if an error occurs, converting the result to an optional. It does not return false or 0, and it does not propagate an uncaught error. This helps safely handle errors in a concise way.

  6. Behavior of 'try!' keyword

    What happens if you use 'try!' with a throwing function that actually throws an error?

    1. The error is caught automatically
    2. A warning is generated, but code continues
    3. The program crashes at runtime
    4. The error is silently ignored

    Explanation: 'try!' forcefully executes a throwing function and assumes no error will occur; if one is thrown, the program crashes. The error is not ignored or caught automatically, and Swift does not simply emit a warning. It is a risky way to handle error-prone functions.

  7. Declaring custom error types

    What must custom error types in Swift conform to in order to be thrown?

    1. ThrowType
    2. Error protocol
    3. Throwable
    4. Exception

    Explanation: Custom error types in Swift must conform to the Error protocol to be used with throw. 'Throwable', 'Exception', and 'ThrowType' are not recognized interfaces or keywords in Swift. The Error protocol ensures that conforming types can be thrown and caught properly.

  8. Placing error handling logic

    Where should you place code that may throw an error in Swift to handle the possible error properly?

    1. Inside a 'repeat' block
    2. After an 'if let' statement
    3. Inside the 'do' block of a 'do-catch' statement
    4. Directly after a 'guard' statement

    Explanation: Code that may throw an error should be placed inside the 'do' block of a 'do-catch' statement to catch and handle errors effectively. 'repeat' blocks and 'guard' or 'if let' statements are not designed for direct error handling. Only the 'do-catch' structure provides the required mechanism.

  9. Catching specific errors

    Which part of the 'do-catch' structure allows you to handle specific error types differently?

    1. The catch clauses with pattern matching
    2. The function's return type
    3. The try keyword
    4. The throw statement

    Explanation: Swift's 'catch' clauses inside a 'do-catch' block can use pattern matching to respond to different error types or values. The 'throw' statement only generates errors but does not handle them, and function return types or 'try' do not relate to customizing error handling. Pattern matching in 'catch' provides flexibility.

  10. Optional error handling with 'try?'

    Why might a developer use 'try?' instead of 'try' or 'try!' when calling a throwing function?

    1. To receive an optional result without having to write error handling code
    2. To automatically rethrow the error
    3. To guarantee that no error will ever be thrown
    4. To force the function to return a non-optional value

    Explanation: 'try?' is used when a developer prefers to get an optional result (nil on error) and wants to avoid explicit error handling code. It does not guarantee error absence, nor does it force a non-optional return or automatically rethrow errors. This approach is for cases where errors can be safely ignored.