Bird
0
0

A microservice uses this code snippet for graceful degradation:

medium📝 Analysis Q14 of 15
Microservices - Resilience Patterns
A microservice uses this code snippet for graceful degradation:
try {
  data = fetchFromService()
} catch (Exception e) {
  data = null
}
return data.toString()

What is the main problem with this code?
AIt does not handle exceptions properly
BIt returns null.toString() causing a runtime error
CIt always returns an empty string
DIt retries the service call infinitely
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception handling and return statement

    If fetchFromService() fails, data is set to null, then data.toString() is called.
  2. Step 2: Identify the error caused by calling toString() on null

    Calling toString() on null causes a runtime NullPointerException or similar error.
  3. Final Answer:

    It returns null.toString() causing a runtime error -> Option B
  4. Quick Check:

    Calling toString() on null causes error [OK]
Quick Trick: Calling method on null causes runtime error [OK]
Common Mistakes:
MISTAKES
  • Ignoring null check before toString()
  • Assuming exception is handled fully
  • Thinking it retries infinitely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes