Bird
0
0

Consider this @ControllerAdvice class snippet:

medium📝 Debug Q14 of 15
Spring Boot - Exception Handling
Consider this @ControllerAdvice class snippet:
@ExceptionHandler(Exception.class)
public ResponseEntity handleAll(Exception ex) {
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred");
}
Why might this cause problems in a Spring Boot app?
AIt only handles NullPointerExceptions, ignoring other exceptions.
BIt returns 200 OK status instead of error status.
CIt causes syntax errors because @ExceptionHandler cannot handle Exception class.
DIt catches all exceptions, hiding specific error handlers and making debugging harder.
Step-by-Step Solution
Solution:
  1. Step 1: Understand broad exception handling impact

    Handling Exception.class catches all exceptions, which can override more specific handlers and hide detailed error info.
  2. Step 2: Evaluate other options

    It only handles NullPointerExceptions, ignoring other exceptions. is wrong because it handles all exceptions, not just NullPointerException. It causes syntax errors because @ExceptionHandler cannot handle Exception class. is incorrect; syntax is valid. It returns 200 OK status instead of error status. is wrong; status is 500, not 200.
  3. Final Answer:

    It catches all exceptions, hiding specific error handlers and making debugging harder. -> Option D
  4. Quick Check:

    Generic Exception handler can hide specific errors [OK]
Quick Trick: Generic handlers override specific ones, use carefully [OK]
Common Mistakes:
  • Thinking it only handles NullPointerException
  • Believing it causes syntax errors
  • Assuming it returns 200 OK status

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes