Bird
0
0

Why is @ControllerAdvice preferred over try-catch blocks inside each controller method for exception handling?

hard📝 Conceptual Q10 of 15
Spring Boot - Exception Handling
Why is @ControllerAdvice preferred over try-catch blocks inside each controller method for exception handling?
AIt automatically fixes exceptions without developer intervention
BIt improves application performance by skipping exceptions
CIt centralizes error handling, reducing code duplication and improving maintainability
DIt replaces the need for logging errors
Step-by-Step Solution
Solution:
  1. Step 1: Understand the benefit of centralized handling

    @ControllerAdvice centralizes exception handling for all controllers in one place.
  2. Step 2: Compare with try-catch in each method

    Try-catch in every method causes repeated code and harder maintenance.
  3. Final Answer:

    It centralizes error handling, reducing code duplication and improving maintainability -> Option C
  4. Quick Check:

    Centralized handling improves maintainability [OK]
Quick Trick: Centralize error handling to avoid repetition [OK]
Common Mistakes:
  • Thinking it auto-fixes errors
  • Assuming performance boost
  • Ignoring logging importance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes