Close
« Tutorial

Spring MVC - SimpleMappingExceptionResolver MCQ

0/13
0 ✓
0 ✗

1. What is the main job of SimpleMappingExceptionResolver?

2. Which interface does SimpleMappingExceptionResolver implement (directly or through its parent class)?

3. Which method is used to set the default view shown when an exception has no specific mapping?

4. Which method is used to set the HTTP status code that is sent when the default error view is shown?

5. What does addStatusCode("myView", 404) do?

6. What type of object is passed to setExceptionMappings() to map exception class names to view names?

7. Which code correctly registers a SimpleMappingExceptionResolver in a Java config class?

@EnableWebMvc
@Configuration
public class AppConfig
        implements WebMvcConfigurer {

    @Override
    public void ______(
            List<HandlerExceptionResolver> list) {
        SimpleMappingExceptionResolver r =
                new SimpleMappingExceptionResolver();
        list.add(r);
    }
}

8. What will be the view name used for a NullPointerException in this configuration?

Properties p = new Properties();
p.setProperty(
    NullPointerException.class.getName(),
    "npeView");

SimpleMappingExceptionResolver r =
    new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.setDefaultErrorView("errorView");

9. A handler throws IllegalStateException. What view and status code are used in this configuration?

Properties p = new Properties();
p.setProperty(
    NullPointerException.class.getName(),
    "npeView");

SimpleMappingExceptionResolver r =
    new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.addStatusCode("npeView", 404);
r.setDefaultErrorView("errorView");
r.setDefaultStatusCode(400);

10. A handler throws NullPointerException. What view and status code are used in this configuration?

Properties p = new Properties();
p.setProperty(
    NullPointerException.class.getName(),
    "npeView");

SimpleMappingExceptionResolver r =
    new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.addStatusCode("npeView", 404);
r.setDefaultErrorView("errorView");
r.setDefaultStatusCode(400);

11. In a view page, which expression shows the exception object by default?

<html>
<body>
  <p>Error: ______</p>
</body>
</html>

12. Which method changes the default model attribute name used for the exception object?

13. What will the browser see when this handler is called, if RuntimeException has no mapping and the default error view is configured?

@ResponseBody
@RequestMapping("/test")
public void test() {
    throw new RuntimeException("oops");
}

14. A handler uses Integer.parseInt(id). What happens when id is abc, if NumberFormatException is mapped to badIdView?

@ResponseBody
@RequestMapping("/item/{id}")
public String item(@PathVariable String id) {
    return "Item " + Integer.parseInt(id);
}

// resolver mapping:
// NumberFormatException -> badIdView

15. Which statement about the exception mapping is correct?

Join