Spring MVC - HandlerExceptionResolverComposite MCQ
0/13
0✓
0✗
1. What is the main purpose of HandlerExceptionResolverComposite?
Why:HandlerExceptionResolverComposite holds a list of HandlerExceptionResolver objects and uses them together as a single resolver.
2. Which method is used to provide a list of resolvers to a composite resolver?
HandlerExceptionResolverComposite c =
new HandlerExceptionResolverComposite();
c.____(List.of(r1, r2));
Why:setExceptionResolvers(List<HandlerExceptionResolver>) is the method that provides the resolvers to the composite.
3. A composite has several resolvers in its list. How does Spring select the result from them?
Why: The composite asks the resolvers one by one in list order. A resolver that cannot handle the exception returns null and is skipped. The first resolver that returns a non-null ModelAndView wins and the rest are not asked.
4. What is the output of this code?
HandlerExceptionResolver a = (rq, rs, h, ex) -> null;
HandlerExceptionResolver b = (rq, rs, h, ex) -> null;
HandlerExceptionResolverComposite c =
new HandlerExceptionResolverComposite();
c.setExceptionResolvers(List.of(a, b));
ModelAndView mv =
c.resolveException(req, res, null, new Exception());
System.out.println(mv);
Why: When none of the resolvers returns a non-null ModelAndView, the composite returns null.
5. A resolver in the composite's list implements Ordered. Does its getOrder() value change the order in which the composite tries the resolvers?
Why: The composite does not sort its resolvers. It uses the insertion order of the list. The order value is used elsewhere, when the framework arranges resolvers before putting them in a composite.
6. A controller throws an IllegalStateException. What view name is selected?
HandlerExceptionResolver custom = (rq, rs, h, ex) -> null;
SimpleMappingExceptionResolver s =
new SimpleMappingExceptionResolver();
s.setDefaultErrorView("fallback");
Properties p = new Properties();
p.setProperty(IllegalAccessException.class.getName(), "denied");
s.setExceptionMappings(p);
HandlerExceptionResolverComposite c =
new HandlerExceptionResolverComposite();
c.setExceptionResolvers(List.of(custom, s));
ModelAndView mv =
c.resolveException(null, null, null, new IllegalStateException());
System.out.println(mv.getViewName());
Why: The first resolver returns null, so the next one is tried. IllegalStateException has no specific mapping, so the default error view fallback is used.
7. Which interface does HandlerExceptionResolverComposite implement, so that it can be used like any other exception resolver?
Why: The composite itself is a HandlerExceptionResolver, so it can be added to the list of resolvers just like a single resolver.
8. What does this code do?
@Override
public void extendHandlerExceptionResolvers(
List<HandlerExceptionResolver> resolvers) {
......
resolvers.add(composite);
}
Why: The method receives the current list of resolvers. Calling add() puts the composite in that list along with the existing resolvers.