IllegalArgumentException. There is an @ExceptionHandler method for IllegalArgumentException in the same controller. What will be the HTTP response status when the exception is thrown?import org.springframework.web.bind.annotation.*; import org.springframework.http.ResponseEntity; @RestController @RequestMapping("/api") public class SampleController { @GetMapping("/test") public String test() { throw new IllegalArgumentException("Invalid argument"); } @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) { return ResponseEntity.badRequest().body("Error: " + ex.getMessage()); } }
@ExceptionHandler method returns and how Spring uses it.The @ExceptionHandler method catches the IllegalArgumentException thrown by the controller method. It returns a ResponseEntity with status 400 (Bad Request) and a custom message. Spring uses this to build the HTTP response.
NullPointerException in a Spring Boot controller using @ExceptionHandler. Which method signature is valid?The @ExceptionHandler method must declare a parameter of the exception type it handles or a superclass. Option D correctly declares NullPointerException as a parameter and returns a String view name or response body.
Option D lacks the exception parameter, so it won't receive the exception instance.
Option D declares a parameter of type Exception but the annotation specifies NullPointerException, which is allowed but the return type is raw ResponseEntity without generics, which is discouraged.
Option D lacks the exception parameter, so it is invalid.
@ExceptionHandler in the controller not handle the exception thrown in the service?import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Service; @RestController @RequestMapping("/api") public class DemoController { private final DemoService service; public DemoController(DemoService service) { this.service = service; } @GetMapping("/call") public String callService() { service.doWork(); return "done"; } @ExceptionHandler(IllegalStateException.class) public String handleIllegalState(IllegalStateException ex) { return "Handled: " + ex.getMessage(); } } @Service public class DemoService { public void doWork() { throw new IllegalStateException("Service error"); } }
The exception is thrown inside the callService() controller method when it calls service.doWork(). Since the exception propagates back to the controller method, the @ExceptionHandler in the controller catches it and returns the custom message.
Option C correctly states this behavior.
@ExceptionHandler methods apply when defined inside a controller class?@ExceptionHandler methods defined inside a controller class handle exceptions thrown by request handling methods of that same controller only.
To handle exceptions globally across controllers, you use @ControllerAdvice.
@ExceptionHandler methods, what will be the HTTP response body when NullPointerException is thrown?import org.springframework.web.bind.annotation.*; import org.springframework.http.ResponseEntity; @RestController @RequestMapping("/api") public class MultiHandlerController { @GetMapping("/trigger") public String trigger() { throw new NullPointerException("Null error"); } @ExceptionHandler(RuntimeException.class) public ResponseEntity<String> handleRuntime(RuntimeException ex) { return ResponseEntity.status(500).body("Runtime: " + ex.getMessage()); } @ExceptionHandler(NullPointerException.class) public ResponseEntity<String> handleNull(NullPointerException ex) { return ResponseEntity.status(400).body("Null: " + ex.getMessage()); } }
Spring selects the most specific @ExceptionHandler method matching the thrown exception. Since NullPointerException is a subclass of RuntimeException, the handler for NullPointerException is chosen over the more general RuntimeException handler.
Therefore, the response body is 'Null: Null error' with HTTP status 400.