Why:@ExceptionHandler marks a method that handles exceptions. It can return custom error content to the caller.
2. Where do we normally put @ExceptionHandler inside a controller?
Why:@ExceptionHandler is used on controller methods that are not themselves request handler methods, which means they have no @RequestMapping or its variants.
3. A controller has only one exception handler, declared for Exception. A request method in the same controller throws a RuntimeException. What happens?
Why: If no more specific handler exists, a handler for a superclass can handle the exception. Exception is the superclass of RuntimeException.
4. Which two attribute names of @ExceptionHandler can be used to give the handled exception types? They are interchangeable.
Why: The value attribute is aliased by exception (since Spring 6.2). Both specify the exception types handled by the method.
5. What does the produces attribute of @ExceptionHandler do?
Why: Since Spring 6.2, produces is useful when several handlers match the same exception but should return different content types.
6. The annotation below has no exception type. Which exception does this method handle?
Why: When the annotation value is empty, Spring uses the exception type declared in the method parameter list. At most one exception type is allowed there.
7. Since Spring 6, which return type can an exception handler method use to produce a standard RFC 9457 problem details response?
Why: An exception handler can return a ProblemDetail (or an ErrorResponse) to create a standard problem details error body.
8. An @ExceptionHandler method is defined directly inside a controller class. Which exceptions does it handle?
Why: A handler inside a controller only works for that controller. To share handlers between controllers, put them in a controller advice class.
9. We want these exception handler methods to apply to all controllers. Which annotation should we put on the class?
public class GlobalErrors {
@ExceptionHandler(Exception.class)
@ResponseBody
public String handle(Exception e) {
return "Error";
}
}
Why:@ControllerAdvice makes the handler methods apply globally. For REST APIs, @RestControllerAdvice can be used.
10. Both methods below are in the same controller. What problem does this cause?
@ExceptionHandler(IllegalStateException.class)
@ResponseBody
public String first(IllegalStateException e) {
return "first";
}
@ExceptionHandler(IllegalStateException.class)
@ResponseBody
public String second(IllegalStateException e) {
return "second";
}
Why: Mapping the same exception type to more than one @ExceptionHandler method in the same scope is ambiguous and causes an IllegalStateException.
11. Which class resolves exceptions and invokes @ExceptionHandler methods by default?
Why:ExceptionHandlerExceptionResolver is a HandlerExceptionResolver. It is configured with the DispatcherServlet by default, so no extra setup is needed.
12. Which exceptions can the exception parameter of an @ExceptionHandler method match?
Why: The exception argument can match the top-level exception or any exception in the cause chain, not just the outermost one.
13. What does a Model parameter contain when it is used in an @ExceptionHandler method?
Why: The Model passed to an exception handler is not filled with regular model attributes, so it is always empty.
14. A client sends a request to /test/abc. What is the response body?