0
0
Spring Bootframework~20 mins

@ExceptionHandler in controllers in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Handler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a controller method throws an exception handled by @ExceptionHandler?
Consider a Spring Boot controller with a method that throws IllegalArgumentException. There is an @ExceptionHandler method for IllegalArgumentException in the same controller. What will be the HTTP response status when the exception is thrown?
Spring Boot
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());
    }
}
AThe response status is 400 Bad Request with body 'Error: Invalid argument'.
BThe response status is 500 Internal Server Error with default error page.
CThe response status is 200 OK with empty body.
DThe response status is 404 Not Found because the exception is not handled globally.
Attempts:
2 left
💡 Hint
Think about what the @ExceptionHandler method returns and how Spring uses it.
📝 Syntax
intermediate
2:00remaining
Which @ExceptionHandler method signature is correct for handling NullPointerException?
You want to handle NullPointerException in a Spring Boot controller using @ExceptionHandler. Which method signature is valid?
A
@ExceptionHandler(NullPointerException.class)
public ResponseEntity&lt;?&gt; handleNullPointer(Exception ex) { return ResponseEntity.ok().build(); }
B
@ExceptionHandler
public void handleNullPointer() { }
C
@ExceptionHandler(NullPointerException.class)
public String handleNullPointer() { return "error"; }
D
@ExceptionHandler(NullPointerException.class)
public String handleNullPointer(NullPointerException ex) { return "error"; }
Attempts:
2 left
💡 Hint
The method must accept the exception type or a superclass as a parameter.
🔧 Debug
advanced
2:00remaining
Why does this @ExceptionHandler method not catch exceptions thrown in a service class?
Given this controller and service, why does the @ExceptionHandler in the controller not handle the exception thrown in the service?
Spring Boot
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");
    }
}
ABecause the exception is thrown outside the controller method, the @ExceptionHandler does not catch it.
BBecause the exception is thrown during the controller method execution, @ExceptionHandler should catch it but the service is not a Spring bean.
CBecause the exception is thrown inside the controller method call, the @ExceptionHandler will catch it as expected.
DBecause the service method throws a checked exception, @ExceptionHandler only catches runtime exceptions.
Attempts:
2 left
💡 Hint
Think about where the exception is thrown and how Spring dispatches exceptions to handlers.
🧠 Conceptual
advanced
2:00remaining
What is the scope of @ExceptionHandler methods in Spring Boot controllers?
Where do @ExceptionHandler methods apply when defined inside a controller class?
AThey handle exceptions only thrown by service classes injected into the controller.
BThey handle exceptions only thrown by methods within the same controller class.
CThey handle exceptions thrown by any Spring bean in the application context.
DThey handle exceptions thrown by any controller in the application.
Attempts:
2 left
💡 Hint
Think about the difference between controller-level and global exception handling.
state_output
expert
2:00remaining
What is the HTTP response body when multiple @ExceptionHandler methods match an exception?
Given this controller with two @ExceptionHandler methods, what will be the HTTP response body when NullPointerException is thrown?
Spring Boot
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());
    }
}
AResponse body is 'Null: Null error' with HTTP status 400.
BResponse body is 'Runtime: Null error' with HTTP status 500.
CResponse body is 'Null: Null error' with HTTP status 500.
DResponse body is 'Runtime: Null error' with HTTP status 400.
Attempts:
2 left
💡 Hint
Spring chooses the most specific exception handler available.