0
0
Kafkadevops~10 mins

Error handling in clients in Kafka - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch exceptions when sending a message.

Kafka
try {
    producer.send(record).get();
} catch ([1] e) {
    e.printStackTrace();
}
Drag options to blanks, or click blank then click option'
AInterruptedException
BExecutionException
CTimeoutException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Using IOException which is unrelated to Kafka send failures.
Catching TimeoutException instead of ExecutionException.
2fill in blank
medium

Complete the code to handle interrupted exceptions during send.

Kafka
try {
    producer.send(record).get();
} catch (ExecutionException e) {
    e.printStackTrace();
} catch ([1] e) {
    Thread.currentThread().interrupt();
}
Drag options to blanks, or click blank then click option'
AIOException
BTimeoutException
CKafkaException
DInterruptedException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching TimeoutException instead of InterruptedException.
Not restoring the thread interrupt status.
3fill in blank
hard

Fix the error in the catch block to handle Kafka exceptions correctly.

Kafka
try {
    producer.send(record).get();
} catch ([1] e) {
    System.err.println("Kafka error: " + e.getMessage());
}
Drag options to blanks, or click blank then click option'
AExecutionException
BKafkaException
CInterruptedException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching KafkaException which is not directly thrown by get().
Catching IOException which is unrelated.
4fill in blank
hard

Fill both blanks to create a retry loop that handles send failures.

Kafka
int retries = 0;
boolean sent = false;
while (!sent && retries < 3) {
    try {
        producer.send(record).get();
        sent = true;
    } catch ([1] e) {
        retries++;
        System.out.println("Retry " + retries);
        Thread.sleep([2]);
    }
}
Drag options to blanks, or click blank then click option'
AExecutionException
B1000
C500
DInterruptedException
Attempts:
3 left
💡 Hint
Common Mistakes
Using InterruptedException which handles interruptions rather than send failures.
Using too long or zero sleep time.
5fill in blank
hard

Fill all three blanks to log error details and rethrow the exception.

Kafka
try {
    producer.send(record).get();
} catch ([1] e) {
    logger.error("Send failed: {}", [2]);
    throw new [3](e);
}
Drag options to blanks, or click blank then click option'
AExecutionException
Be.getMessage()
CRuntimeException
DInterruptedException
Attempts:
3 left
💡 Hint
Common Mistakes
Logging the exception object directly without calling getMessage().
Throwing InterruptedException which is unrelated here.