What is the main benefit of using centralized error handling in a Spring Boot application?
Think about how handling errors in one place affects code consistency and maintenance.
Centralized error handling ensures that all errors are managed uniformly, making the code easier to maintain and the responses consistent for users.
Given a Spring Boot application with a @ControllerAdvice class handling exceptions, what happens when a controller method throws an exception?
Consider how @ControllerAdvice works with exceptions thrown in controllers.
The @ControllerAdvice class intercepts exceptions thrown by controller methods and handles them if the exception type matches the handler method.
What will be the HTTP status code and response body when a ResourceNotFoundException is thrown and handled by this centralized error handler?
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity<String> handleNotFound(ResourceNotFoundException ex) { return new ResponseEntity<>("Resource not found: " + ex.getMessage(), HttpStatus.NOT_FOUND); } }
Look at the status code used in the ResponseEntity returned by the handler.
The handler returns a 404 status with a message including the exception details, indicating the resource was not found.
Which option contains a syntax error in defining a centralized error handler method in Spring Boot?
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.http.ResponseEntity; import org.springframework.http.HttpStatus; @ControllerAdvice public class ErrorHandler { @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) { return new ResponseEntity<>("Invalid argument", HttpStatus.BAD_REQUEST); } }
Check the end of the return statement line carefully.
The return statement is missing a semicolon, causing a syntax error.
Given this Spring Boot centralized error handler, why are exceptions not being caught as expected?
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.http.ResponseEntity; import org.springframework.http.HttpStatus; @ControllerAdvice public class GlobalErrorHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleAllExceptions(Exception ex) { return new ResponseEntity<>("Error occurred", HttpStatus.INTERNAL_SERVER_ERROR); } }
Check if the method parameter type matches the exception type in the annotation.
The method parameter type must be the same or a superclass of the exception type in the annotation. Here, Exception.class is declared but method expects RuntimeException, so checked exceptions won't be caught.