Close
« Tutorial

Spring MVC - Custom HandlerExceptionResolver MCQ

0/13
0 ✓
0 ✗

1. What is the main purpose of creating a custom HandlerExceptionResolver?

2. Which method must be implemented when creating a custom HandlerExceptionResolver?

3. What does the resolveException method return to tell Spring MVC to continue with the default exception processing?

4. What does a HandlerExceptionResolver usually return when it has handled the exception by showing an error page?

5. A resolver needs to know which controller method threw the exception. Which resolveException parameter gives this information?

6. Which class is used to read an annotation from the controller method that threw the exception?

7. How can a custom resolver change the HTTP status code of the error response?

8. Which retention policy must a custom annotation have if a resolver needs to read it while the application is running?

9. Which method of WebMvcConfigurer adds a custom resolver without removing the default ones?

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());
    }
}

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;
}

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;
}

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;
}

14. A request to /test throws an exception and this resolver handles it. What HTTP status does the client get?

public ModelAndView resolveException(
        HttpServletRequest req,
        HttpServletResponse res,
        Object handler,
        Exception ex) {

    ModelAndView mv = new ModelAndView("err");
    mv.addObject("error", ex);
    return mv;
}

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?

Join