Bird
0
0

Which design best supports a centralized error handler in Spring Boot that returns varied HTTP statuses for different exceptions and logs error details?

hard📝 Conceptual Q8 of 15
Spring Boot - Exception Handling
Which design best supports a centralized error handler in Spring Boot that returns varied HTTP statuses for different exceptions and logs error details?
ADefine exception handling inside each controller with custom logging logic
BHandle all exceptions in one method returning HTTP 500 and log only critical errors
CUse multiple @ExceptionHandler methods in a @ControllerAdvice class, each logging and returning specific status codes
DUse a filter to catch exceptions and always return HTTP 200 with error details in the body
Step-by-Step Solution
Solution:
  1. Step 1: Requirement analysis

    We need different HTTP statuses per exception type and logging of error messages.
  2. Step 2: Best practice

    Multiple @ExceptionHandler methods inside a @ControllerAdvice class allow tailored responses and logging.
  3. Step 3: Why others fail

    One method with HTTP 500 ignores specific statuses; controller-level handling duplicates code; filters can't easily customize responses per exception.
  4. Final Answer:

    Use multiple @ExceptionHandler methods in a @ControllerAdvice class, each logging and returning specific status codes -> Option C
  5. Quick Check:

    Multiple handlers enable specific responses and logging [OK]
Quick Trick: Multiple handlers in @ControllerAdvice for specific errors [OK]
Common Mistakes:
  • Using a single handler for all exceptions with generic status
  • Handling exceptions only inside controllers
  • Returning HTTP 200 for errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes