0
0
Spring Bootframework~20 mins

@ControllerAdvice for global handling in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ControllerAdvice Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an exception is thrown in a controller with @ControllerAdvice?
Consider a Spring Boot application with a @ControllerAdvice class that handles NullPointerException globally. What will happen if a controller method throws a NullPointerException?
Spring Boot
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.ResponseEntity;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<String> handleNullPointer(Exception ex) {
        return ResponseEntity.status(400).body("Null pointer error caught globally");
    }
}

// In some controller:
// public String someMethod() {
//     throw new NullPointerException();
// }
AThe global handler method handleNullPointer will catch the exception and return a 400 response with the message.
BThe exception will propagate and cause a server error 500 without using the global handler.
CThe application will crash because @ControllerAdvice cannot handle NullPointerException.
DThe exception will be ignored and the controller will return a successful empty response.
Attempts:
2 left
💡 Hint
Think about how @ControllerAdvice works for exceptions thrown in controllers.
📝 Syntax
intermediate
1:30remaining
Which @ExceptionHandler method signature is correct inside @ControllerAdvice?
You want to handle IllegalArgumentException globally. Which method signature is valid inside a @ControllerAdvice class?
Apublic ResponseEntity<String> handleIllegalArg(IllegalArgumentException ex)
Bpublic void handleIllegalArg() throws IllegalArgumentException
Cpublic String handleIllegalArg(Exception ex, HttpServletRequest req)
Dpublic int handleIllegalArg(IllegalArgumentException ex)
Attempts:
2 left
💡 Hint
The method should accept the exception type and return a response type.
🔧 Debug
advanced
2:00remaining
Why is the global exception handler not catching exceptions?
Given this @ControllerAdvice class, exceptions thrown in controllers are not caught. What is the likely cause? @ControllerAdvice public class GlobalHandler { @ExceptionHandler(RuntimeException.class) public String handleRuntime(RuntimeException ex) { return "Error caught"; } } Controllers throw IllegalArgumentException which extends RuntimeException.
AThe @ExceptionHandler method must handle IllegalArgumentException explicitly, not RuntimeException.
BThe exception is not caught because IllegalArgumentException is not a RuntimeException.
CThe @ControllerAdvice class must be annotated with @Component to be detected.
DThe @ControllerAdvice class is missing @RestControllerAdvice annotation to handle REST responses.
Attempts:
2 left
💡 Hint
Consider the difference between @ControllerAdvice and @RestControllerAdvice for response bodies.
🧠 Conceptual
advanced
1:30remaining
What is the scope of @ControllerAdvice in a Spring Boot app?
Which statement best describes the scope of a class annotated with @ControllerAdvice?
A@ControllerAdvice applies only to controllers in the same package as the advice class.
B@ControllerAdvice applies globally to all controllers in the application by default.
C@ControllerAdvice applies only to controllers explicitly listed in its annotations.
D@ControllerAdvice applies only to controllers annotated with @RestController.
Attempts:
2 left
💡 Hint
Think about the default behavior of @ControllerAdvice without filters.
state_output
expert
1:00remaining
What is the HTTP status code returned by this global handler?
Given this @ControllerAdvice handler: @ControllerAdvice public class GlobalErrorHandler { @ExceptionHandler(IllegalStateException.class) public ResponseEntity handleIllegalState(IllegalStateException ex) { return ResponseEntity.status(409).body("Conflict error"); } } If a controller throws IllegalStateException, what HTTP status code will the client receive?
A400
B500
C409
D200
Attempts:
2 left
💡 Hint
Look at the ResponseEntity.status() call in the handler.