Bird
0
0

Consider this custom exception handler class:

medium📝 Debug Q14 of 15
Spring Boot - Exception Handling
Consider this custom exception handler class:
  @ControllerAdvice
  public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

      @ExceptionHandler(NullPointerException.class)
      public ResponseEntity handleNullPointer(NullPointerException ex, WebRequest request) {
          String body = "Null value found";
          return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
      }

      @Override
      protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
          String body = "Validation failed";
          return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
      }
  }
If a NullPointerException occurs, what will be the HTTP status code returned?
A500 Internal Server Error
B400 Bad Request
C404 Not Found
D200 OK
Step-by-Step Solution
Solution:
  1. Step 1: Identify the handler for NullPointerException

    The method annotated with @ExceptionHandler(NullPointerException.class) returns status INTERNAL_SERVER_ERROR.
  2. Step 2: Map INTERNAL_SERVER_ERROR to HTTP status code

    INTERNAL_SERVER_ERROR corresponds to HTTP status 500.
  3. Final Answer:

    500 Internal Server Error -> Option A
  4. Quick Check:

    NullPointerException handled with 500 status [OK]
Quick Trick: Check @ExceptionHandler annotation for exception type [OK]
Common Mistakes:
  • Assuming validation error handler is called for NullPointerException
  • Confusing 400 Bad Request with 500 Internal Server Error
  • Ignoring @ExceptionHandler methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes