1. What is the main purpose of creating a custom HandlerExceptionResolver?
Why: A custom HandlerExceptionResolver lets you add your own exception handling logic on top of what Spring MVC already provides.
2. Which method must be implemented when creating a custom HandlerExceptionResolver?
Why: The HandlerExceptionResolver interface has a single method named resolveException, which receives the request, response, handler and the exception.
3. What does the resolveException method return to tell Spring MVC to continue with the default exception processing?
Why: Returning null means this resolver did not handle the exception, so Spring MVC tries the next resolver or the default processing.
4. What does a HandlerExceptionResolver usually return when it has handled the exception by showing an error page?
Why: The resolver returns a ModelAndView that holds the error view name and any data the error page needs.
5. A resolver needs to know which controller method threw the exception. Which resolveException parameter gives this information?
Why: The handler argument is the object that was executing when the exception happened. For annotated controllers it is a HandlerMethod, which gives access to the method and its annotations.
6. Which class is used to read an annotation from the controller method that threw the exception?
Why:HandlerMethod provides the getMethodAnnotation() method, which returns the annotation of the given type or null if it is not present.
7. How can a custom resolver change the HTTP status code of the error response?
Why: The resolver has access to the HttpServletResponse, so it can call setStatus() before returning the ModelAndView.
8. Which retention policy must a custom annotation have if a resolver needs to read it while the application is running?
Why: Only annotations with RUNTIME retention are available through reflection while the application runs.
9. Which method of WebMvcConfigurer adds a custom resolver without removing the default ones?
Why:extendHandlerExceptionResolvers gives you the list of existing resolvers so you can add yours. configureHandlerExceptionResolvers replaces the default list when you add to it.
10. What is the problem with the following configuration?
@Configuration
@EnableWebMvc
public class AppConfig
implements WebMvcConfigurer {
@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> list) {
list.add(new MyResolver());
}
}
Why: When the list in configureHandlerExceptionResolvers is not empty, Spring does not add its default resolvers. Use extendHandlerExceptionResolvers to keep them.
11. Complete the resolver so that it handles only exceptions from methods annotated with @ErrorView.
public ModelAndView resolveException(
HttpServletRequest req,
HttpServletResponse res,
Object handler,
Exception ex) {
if (handler instanceof HandlerMethod hm) {
ErrorView ev = hm.getMethodAnnotation(
ErrorView.class);
if (/* condition */) {
res.setStatus(ev.status().value());
return new ModelAndView(ev.value());
}
}
return null;
}
Why:getMethodAnnotation() returns null if the annotation is missing. The resolver handles the exception only when ev != null; otherwise it returns null for default processing.
12. A resolver is registered. Controller method a() has @ErrorView("err") and throws an exception. Method b() has no annotation and throws an exception. What happens?
@ErrorView("err")
@RequestMapping("/a")
public String a() throws Exception {
throw new Exception("A");
}
@RequestMapping("/b")
public String b() throws Exception {
throw new Exception("B");
}
public ModelAndView resolveException(
HttpServletRequest req,
HttpServletResponse res,
Object handler,
Exception ex) {
if (handler instanceof HandlerMethod hm) {
ErrorView ev = hm.getMethodAnnotation(
ErrorView.class);
if (ev != null) {
return new ModelAndView(ev.value());
}
}
return null;
}
Why: The resolver handles only methods that have the annotation. For /b it returns null, so the normal exception processing continues.
13. A handler method is annotated with @ErrorView and its status is set to HttpStatus.GONE. What HTTP response line will the client get?
@ErrorView(value = "oops",
status = HttpStatus.GONE)
@RequestMapping("/test")
public String test() throws Exception {
throw new Exception("failed");
}
public ModelAndView resolveException(
HttpServletRequest req,
HttpServletResponse res,
Object handler,
Exception ex) {
if (handler instanceof HandlerMethod hm) {
ErrorView ev = hm.getMethodAnnotation(
ErrorView.class);
if (ev != null) {
res.setStatus(ev.status().value());
return new ModelAndView(ev.value());
}
}
return null;
}
Why:HttpStatus.GONE has the value 410. The resolver sets this on the response before returning the error view.
14. A request to /test throws an exception and this resolver handles it. What HTTP status does the client get?
Why: The resolver returns a view but never calls response.setStatus(). Since the exception counts as handled, the response keeps the default status 200 OK. To send an error status, the resolver must set it explicitly.
15. A controller has an @ExceptionHandler method for IOException, and a custom resolver is also registered. By default, which one gets the first chance to handle the exception?
Why: Resolvers run in order. Spring's built-in resolver for @ExceptionHandler is first in the list, and extendHandlerExceptionResolvers adds your resolver after it.