B. Catch block should catch Exception, not KafkaException
C. Missing return statement after catch
D. producer.close() should be inside finally block
Solution
Step 1: Check resource cleanup
producer.close() must run regardless of exceptions to free resources.
Step 2: Use finally block for cleanup
Placing close() inside finally ensures it always executes.
Final Answer:
producer.close() should be inside finally block -> Option D
Quick Check:
Always close resources in finally block [OK]
Hint: Put cleanup code in finally block to always run [OK]
Common Mistakes:
Closing producer only on success
Catching too broad or wrong exceptions
Assuming send() never throws exceptions
5. You want to build a Kafka consumer that retries polling up to 3 times on recoverable errors, then logs and stops. Which approach correctly implements this error handling?
hard
A. Use a loop with try-catch around poll; on exception increment retry count; break after 3 retries; finally close consumer
B. Call consumer.poll() once; if exception, immediately close consumer without retry
C. Wrap poll in try-catch but ignore exceptions and continue polling forever
D. Use multiple nested try-catch blocks without retry logic
Solution
Step 1: Understand retry logic requirements
Retry polling up to 3 times on recoverable errors, then stop and log.
Step 2: Identify correct control flow
Loop with try-catch, count retries, break after 3 attempts, and close consumer in finally block is correct.
Final Answer:
Use a loop with try-catch around poll; on exception increment retry count; break after 3 retries; finally close consumer -> Option A
Quick Check:
Retry with loop and try-catch = Use a loop with try-catch around poll; on exception increment retry count; break after 3 retries; finally close consumer [OK]
Hint: Use loop with try-catch and retry count for controlled retries [OK]
Common Mistakes:
Not retrying on recoverable errors
Ignoring exceptions and continuing endlessly
Closing consumer too early without retries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.