What is Spring Boot exception handling?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Spring Boot basics — a prerequisite for any developer role.
Answer
Spring Boot provides several mechanisms for centralizing exception handling in REST APIs: @ControllerAdvice / @RestControllerAdvice: a global exception handler that applies to all controllers: @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex, HttpServletRequest req) { ErrorResponse error = new ErrorResponse(404, ex.getMessage(), req.getRequestURI(), Instant.now()); return ResponseEntity.status(404).body(error); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<ValidationErrorResponse> handleValidation(MethodArgumentNotValidException ex) { Map<String,String> errors = new LinkedHashMap<>(); ex.getBindingResult().getFieldErrors().forEach(e -> errors.put(e.getField(), e.getDefaultMessage())); return ResponseEntity.badRequest().body(new ValidationErrorResponse(errors)); } @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleAll(Exception ex, HttpServletRequest req) { log.error("Unhandled exception", ex); ErrorResponse error = new ErrorResponse(500, "Internal server error", req.getRequestURI(), Instant.now()); return ResponseEntity.status(500).body(error); } }. Custom exceptions: public class ResourceNotFoundException extends RuntimeException { public ResourceNotFoundException(String message) { super(message); } }. @ResponseStatus on exception class: @ResponseStatus(HttpStatus.NOT_FOUND) public class NotFoundException extends RuntimeException. Problem Details (RFC 7807): Spring Boot 3+ supports ProblemDetail response format natively: return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "User not found").
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Spring Boot codebase.