DVA-C02 Question 350
Single answerYou are developing an application using the AWS SDK for Python (Boto3) to interact with an Amazon S3 bucket. During testing, your application throws an exception when attempting to retrieve an object that does not exist in the bucket. Which exception should you handle in your code to address this scenario?
- A
NoSuchKey
- B
AccessDenied
- C
BucketNotFound
- D
ClientError
Show answer and explanation
Correct answer: A
Explanation
When working with the AWS SDK, specific exceptions like NoSuchKey allow developers to handle errors in a more granular way. In this case, when attempting to retrieve a non-existent object from an S3 bucket, the NoSuchKey exception should be handled to properly identify and respond to the issue.
- A. Correct.
NoSuchKey is the correct exception raised by the AWS SDK when attempting to access a non-existent object in an S3 bucket.
- B. Incorrect.
AccessDenied is an exception that occurs when the credentials used do not have the required permissions to access the resource, but it is not relevant to this scenario where the object simply does not exist.
- C. Incorrect.
BucketNotFound is not a valid exception in the AWS SDK. A similar situation would raise a ClientError with a specific error code.
- D. Incorrect.
ClientError is a general exception that serves as a parent for many specific errors (e.g., NoSuchKey), but it is not the most appropriate choice to specifically handle the case of a missing object.