Bird
0
0

Identify the issue in this centralized error handler code snippet:

medium📝 Debug Q7 of 15
Spring Boot - Exception Handling
Identify the issue in this centralized error handler code snippet:
@ControllerAdvice
public class GlobalErrorHandler {
  @ExceptionHandler(Exception.class)
  public ResponseEntity handleException(Exception ex) {
    return new ResponseEntity<>("An error occurred", HttpStatus.OK);
  }
}
AThe method should return void instead of ResponseEntity
BMissing @ResponseBody annotation on the method
C@ExceptionHandler must specify multiple exception classes
DReturning HTTP status OK (200) for an error is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Analyze HTTP status usage

    The handler returns HttpStatus.OK (200) even though an error occurred, which is misleading.
  2. Step 2: Correct status code

    Errors should return appropriate error status codes like 400 or 500 to indicate failure.
  3. Final Answer:

    Returning HTTP status OK (200) for an error is incorrect -> Option D
  4. Quick Check:

    Error responses should not use 200 OK [OK]
Quick Trick: Error responses must not return 200 OK [OK]
Common Mistakes:
  • Assuming @ResponseBody is required inside @ControllerAdvice
  • Thinking @ExceptionHandler must list multiple exceptions
  • Returning void instead of ResponseEntity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes