Test your understanding of structured error handling, the differences between exceptions and return errors, resource cleanup techniques, retry patterns with backoff and idempotency, and mapping errors to HTTP status codes. This quiz covers foundational concepts and best practices in reliable application design.
When a function encounters an unexpected situation, which error handling method interrupts normal control flow automatically and requires a try/catch or similar block?
Explanation: Exceptions raise errors that jump control flow to the nearest handler, so catching them often requires a try/catch or similar structure. Return errors, in contrast, are values returned that must be checked explicitly after each call. Log messages only report problems but do not interrupt flow, and warnings are typically informational without enforcing program structure changes.
Which of the following best describes a 'return error' pattern in function design?
Explanation: The return error pattern involves a function returning an error value (such as a code or object) alongside the normal result, requiring the caller to check for errors. Halting execution is too drastic, exceptions involve control transfer rather than value returns, and logging alone does not communicate error information for handling.
If a file is opened in a function and an error occurs during processing, which language construct ensures the file is always closed regardless of errors?
Explanation: The 'finally' or 'defer' construct is specifically used to guarantee cleanup actions like closing a file, regardless of whether normal execution or an error occurred. An immediate return can skip cleanup if not managed carefully, while continue and break are control statements that do not handle cleanup directly.
Which retry strategy helps prevent overwhelming a system when retrying failed network requests?
Explanation: Exponential backoff gradually increases the wait time between retries, helping reduce the risk of overwhelming the system and providing time for recovery. Immediate retries or constant short delays can worsen network congestion, and retrying only once may not be sufficient for transient failures.
Why is it important for operations like payment processing to be idempotent when implementing automatic retries?
Explanation: Idempotency guarantees that repeating the same operation will not lead to duplicated or harmful effects, which is crucial for actions like payments. The goal is not to make requests fail or faster, and randomness in retries does not address potential side effects from repeats.
When designing an API, which HTTP status code should you use to indicate that a requested resource does not exist?
Explanation: A 404 Not Found accurately signals that the requested resource could not be found. A 500 indicates server errors, 201 is used for successful resource creation, and 301 signifies that a resource has been moved, none of which are suitable for missing resources.
In structured error handling, where should you place a general exception-catching block to prevent program crashes from unforeseen errors?
Explanation: Placing a catch-all at the outermost level helps handle any exceptions not managed elsewhere, preventing unexpected crashes. Catch or error handling after each statement, after variable declarations, or within every loop is inefficient and burdensome.
When returning error messages from an API, what is the best practice for user safety and clarity?
Explanation: A helpful but generic error helps the user understand the issue without revealing sensitive system information. Detailed internal info or jargon can risk security and confuse users, while empty messages aren't informative.
If a function using the 'return error' pattern produces an error, what should the function's non-error return values typically be?
Explanation: When an error occurs, return values should usually be set to defaults (such as null or zero) to indicate no meaningful result. Returning random, duplicate, or unrelated values would create confusion and make error handling less predictable.
Which HTTP status code is most appropriate for informing the user that they have exceeded a rate limit and should try again later?
Explanation: A 429 status code specifically indicates excessive requests and suggests rate limiting. A 200 is for success, 503 means temporary unavailability, and 403 is for permission issues, none of which accurately communicate throttling.