Bird
0
0

Which of the following correctly demonstrates how to handle exceptions inside a Kafka Streams map operation in Java?

easy📝 Syntax Q3 of 15
Kafka - Advanced Stream Processing
Which of the following correctly demonstrates how to handle exceptions inside a Kafka Streams map operation in Java?
Astream.map((key, value) -> { try { return process(value); } catch (Exception e) { return null; } });
Bstream.map((key, value) -> process(value)).catch(Exception e -> handle(e));
Cstream.map((key, value) -> process(value)).onError(e -> log(e));
Dstream.map((key, value) -> process(value)) throws Exception;
Step-by-Step Solution
Solution:
  1. Step 1: Use try-catch inside the lambda

    Kafka Streams does not provide built-in error handling methods like catch or onError on the map method, so exceptions must be caught inside the lambda.
  2. Step 2: Return a fallback or null on exception

    Returning null or a fallback value inside the catch block prevents the stream from failing.
  3. Final Answer:

    stream.map((key, value) -> { try { return process(value); } catch (Exception e) { return null; } }); correctly uses try-catch inside the map lambda.
  4. Quick Check:

    Check for try-catch inside lambda [OK]
Quick Trick: Use try-catch inside lambda for error handling [OK]
Common Mistakes:
MISTAKES
  • Using non-existent catch methods on stream operations
  • Throwing exceptions without catching inside lambdas
  • Assuming Kafka Streams auto-handles exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes