1. What kind of problem does HandlerExceptionResolver deal with?
Why:HandlerExceptionResolver deals with Java exceptions raised while Spring MVC handles a request. It is not used to map HTTP error codes to pages.
2. How is HandlerExceptionResolver different from error handling configured in web.xml?
Why: A web.xml error-page mapping can link an error code or exception type to a page. A HandlerExceptionResolver handles exceptions in Java code and has access to the request, response, handler, and exception, so it is more flexible.
3. How does a HandlerExceptionResolver tell Spring MVC to show a specific error page?
Why: The resolver returns a ModelAndView. It holds the error view name and any model data the error page needs.
4. The handler parameter of resolveException() normally holds which type?
Why: In Spring MVC, the handler is normally a HandlerMethod. It describes the controller method where the exception happened.
5. Which method name should be used to register a custom resolver in the configuration class?
@Configuration
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
@Override
public void ____(List<HandlerExceptionResolver> resolvers) {
resolvers.add(new MyResolver());
}
}
Why:WebMvcConfigurer provides configureHandlerExceptionResolvers(). It receives the list that will replace the default resolvers; adding a resolver there disables the defaults. If you want to keep the default resolvers and add your own, override extendHandlerExceptionResolvers() instead.
6. A JSP view resolver maps view names to /WEB-INF/pages/ with the .jsp suffix. A user opens /pay. What does the browser show?
@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> resolvers) {
resolvers.add((req, res, handler, ex) ->
new ModelAndView("oops")
.addObject("msg", ex.getMessage())
);
}
@GetMapping("/pay")
public void pay() {
throw new IllegalStateException("No funds");
}
Why: The controller throws an exception with the message No funds. The resolver puts that message in the model as msg and returns the oops view, so the page shows Error: No funds.
7. A custom resolver is registered. The interceptor method below throws an exception. What happens?
public boolean preHandle(
HttpServletRequest req,
HttpServletResponse res,
Object handler) throws Exception {
throw new Exception("blocked");
}
Why:resolveException() is also called when an exception is thrown outside the handler method, such as in an interceptor, as long as it happens while handling the request.
8. Since Spring 6.1, what happens by default when a request URL has no matching handler?
Why: Since Spring 6.1, DispatcherServlet raises a NoHandlerFoundException for unmapped URLs by default. It is handled by the normal exception resolver chain like any other exception.
9. By default in Spring versions before 6.1, is resolveException() called for an unmapped URL that gives a 404 error?
Why: Before Spring 6.1, the default was throwExceptionIfNoHandlerFound=false, so a 404 was sent without invoking HandlerExceptionResolver.resolveException(). If throwExceptionIfNoHandlerFound was set to true, a NoHandlerFoundException was raised and could reach the resolver.