Error Handling Essentials: Exceptions, Cleanup, and Robust APIs Quiz

Explore core error handling principles, including effective use of exceptions, resource cleanup strategies, and techniques for building robust APIs that gracefully manage failures. Enhance your understanding of error propagation, custom exceptions, and best practices for resilience in application design.

  1. Handling Unexpected Errors

    When designing an API to handle file operations, why is it advisable to catch only specific exceptions rather than using a generic catch-all for every possible error?

    1. Catching specific exceptions allows the program to run faster by skipping error checks
    2. Catching specific exceptions clarifies error intent and avoids swallowing unexpected bugs
    3. Generic catch-all blocks automatically fix errors without programmer input
    4. A catch-all block is always preferred because it prevents any program stoppage

    Explanation: Catching specific exceptions makes it clear which error conditions are expected and how they are handled, while also preventing accidental hiding of unforeseen issues. The claim that specific exception handling skips error checks and increases speed is not accurate, as exception checks occur regardless. Using a generic catch-all can obscure programming mistakes and make debugging harder, so it is not always the best approach. Additionally, catch-all blocks do not automatically fix errors—they only intercept them, which may lead to unnoticed failures.

  2. Resource Cleanup Techniques

    Which approach ensures that resources such as file handles or network connections are properly released after use, even if an error occurs during execution?

    1. Pre-initializing resources before error handling begins
    2. Placing cleanup code only after all function statements
    3. Encapsulating cleanup in a finally block after try-catch handling
    4. Ignoring cleanup, as resources are always released automatically

    Explanation: A finally block guarantees execution regardless of whether an exception is thrown, making it the best choice for reliable resource cleanup. Placing cleanup code only after all function statements is risky, as exceptions may prevent execution before reaching it. Simply pre-initializing resources does not address safe release if errors occur. Assuming that resources are released automatically can lead to leaks, since many resources require explicit handling.

  3. Raising Custom Exceptions

    In a payment processing API, what is the main advantage of defining and raising custom exception types instead of using a generic exception?

    1. Generic exceptions are more descriptive for all error scenarios
    2. Custom exceptions can only be used once and are hard to recognize
    3. Generic exceptions ensure compatibility with all error handling blocks
    4. Custom exceptions allow for more targeted error handling and clearer diagnostics

    Explanation: Custom exceptions make error handling more precise, as specific types can be caught and handled differently depending on the context. Generic exceptions lack detail and may make debugging harder. The statement that custom exceptions are single-use or hard to recognize is incorrect; well-named exceptions improve code readability. While generic exceptions are broadly compatible, they do not provide the same clarity as custom ones.

  4. Error Propagation Strategies

    If a function catches an exception, logs it, but then re-raises it for the caller to handle, what is the primary benefit of this approach in robust API design?

    1. It eliminates the need for any error handling in the calling code
    2. It preserves the error information and enables higher-level recovery or fallback
    3. It prevents further propagation of exceptions, stopping the program safely
    4. It hides errors entirely from users of the API

    Explanation: By logging and re-raising, the function keeps a record of the problem while still allowing upstream parts of the program to decide how to recover, retry, or report errors. Hiding errors would mean users might not know when failures occur, which is not the case here. Claiming it prevents further propagation is incorrect; re-raising specifically allows further escalation. This strategy does not make the calling code error-proof, as callers must still handle propagated exceptions.

  5. Robust API Error Messages

    Why should an API designed for public use avoid exposing detailed internal error messages or stack traces directly to the consumer?

    1. Detailed internal errors can reveal implementation details and create security risks
    2. Leaving out details prevents all API usage problems
    3. Stack traces automatically fix bugs for API consumers
    4. Detailed errors always confuse developers and make debugging impossible

    Explanation: Exposing internal error messages or stack traces can inadvertently leak sensitive information or system internals, which could be exploited. While some details can be helpful, developers generally benefit from error logs not visible to consumers. Stack traces do not fix problems; they provide context for debugging. Omitting all error details does not prevent usage issues; instead, it may frustrate users due to lack of information.