Quick revision
The short answer
Exception resolvers run in order, and the first one that returns a ModelAndView ends the search. A custom resolver with high precedence runs before ExceptionHandlerExceptionResolver and swallows every exception, so @ExceptionHandler methods stop running; a resolver with no order runs after the three defaults and only sees what they left, so it never runs for exceptions they handle. The fix is to give the resolver a deliberate order and return null for everything it should not handle, or better, to keep application errors in @ControllerAdvice and use the resolver only for cross-cutting work such as logging.
What to remember
In this answer, we saw that the DispatcherServlet asks exception resolvers in order and stops at the first ModelAndView. A custom resolver with high precedence takes over every exception and hides @ExceptionHandler methods and the 4xx mapping of standard exceptions, while one with no order runs after the defaults and only sees leftovers. The fix is to give each resolver a deliberate order and return null for what it does not own: a logging resolver runs first and always passes, and the JSON fallback moves into an advice with ResponseEntityExceptionHandler. MockMvc tests per exception type keep the order from drifting.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Exception resolvers run in order, and the first one that returns a ModelAndView ends the search. A custom resolver with high precedence runs before ExceptionHandlerExceptionResolver and swallows every exception, so @ExceptionHandler methods stop running; a resolver with no order runs after the three defaults and only sees what they left, so it never runs for exceptions they handle. The fix is to give the resolver a deliberate order and return null for everything it should not handle, or better, to keep application errors in @ControllerAdvice and use the resolver only for cross-cutting work such as logging.
Why this question is asked
This bug often appears after a well-meant change: someone wants every error logged and returned as JSON, writes a HandlerExceptionResolver, registers it as a bean, and moves on. Weeks later, an @ExceptionHandler in an advice is ignored for one exception type, while for another the new resolver is never called at all. The two symptoms look unrelated, but they come from one rule about ordering. Interviewers like this question because it can only be answered by someone who knows how the DispatcherServlet walks its resolvers. In this answer, we will recap that walk, reproduce both symptoms, explain them, fix the resolver, and lock the behaviour in with tests.
1A Quick Recap of the Resolver Chain
When a controller throws, the DispatcherServlet asks each HandlerExceptionResolver bean in turn, sorted by order (lower numbers first). A resolver returns a ModelAndView to say the exception is handled, even an empty one meaning it already wrote the response, or null to pass. Spring's three defaults live inside one bean, a HandlerExceptionResolverComposite with order 0: ExceptionHandlerExceptionResolver for @ExceptionHandler methods, ResponseStatusExceptionResolver for @ResponseStatus, and DefaultHandlerExceptionResolver for Spring MVC's standard exceptions. A resolver bean without an explicit order gets Ordered.LOWEST_PRECEDENCE, so it sorts after the composite.
2Reproducing the Two Symptoms
Here is the kind of resolver that causes the trouble. It handles everything, writes JSON and returns an empty ModelAndView.
Notice that the two versions produce exactly the two complaints in the question. In a real codebase both can appear at once, for example when the resolver's order is set from a property that differs between environments, or when two resolvers are registered, one early and one late. It is also common that the resolver's order lands among others with the same precedence, such as Spring Boot's DefaultErrorAttributes, which also uses the highest precedence, and then the relative order between them depends on registration details rather than on intent.
3Why Each Symptom Happens
In version A, our resolver runs before the composite. It returns a ModelAndView for every exception, so the DispatcherServlet stops asking. ExceptionHandlerExceptionResolver never gets a chance, which is why the advice's @ExceptionHandler is ignored. It also hides DefaultHandlerExceptionResolver, so a 405 or a 415 becomes a 500, and the client loses the information it needed.
In version B, our resolver runs after the composite. The composite handles every exception that has a matching @ExceptionHandler, every @ResponseStatus exception and every standard Spring MVC exception, and the search stops there. Our resolver only sees the leftovers, which are mostly unexpected runtime exceptions. If the goal was to log every error, version B silently misses most of them.
There is one more group our resolver never sees in either version: exceptions that happen outside the DispatcherServlet, in servlet filters or in Spring Security's filter chain. Resolvers only run for exceptions thrown while the DispatcherServlet is handling the request, so a failure in a filter goes straight to the container and to /error.
- DefaultErrorAttributeshighest precedence, records the exception, returns null
- Custom resolver with HIGHEST_PRECEDENCE?YesIt handles everything: advice and 405/415 lostNoContinue to the composite
- Composite (order 0)
@ExceptionHandler,@ResponseStatus, standard MVC exceptions - Was the exception handled?YesSearch stops: a late resolver never sees itNoCustom resolver with no order runs
- Nothing handledcontainer dispatches to /error
5 steps in order. Play them, or step through them one at a time.
The same resolver either takes over everything or sees almost nothing, depending only on its order.
4Fixing the Resolver
The fix starts with deciding what the resolver is for. If its job is to log every exception, it should run first and never handle anything: it logs and returns null, so the chain continues as before. If its job is a JSON fallback for exceptions nobody else handled, it should run last, and it should check that the response is not already committed. These are two different jobs, so they become two small classes.
Notice that the fallback became an @ExceptionHandler(Exception.class) in an advice instead of a resolver. Within ExceptionHandlerExceptionResolver, the most specific matching handler wins, so the specific handlers for OrderNotFoundException still apply and only truly unexpected exceptions reach the fallback. One thing to watch: Exception.class also matches Spring MVC's standard exceptions, so the fallback advice should extend ResponseEntityExceptionHandler, which handles those with the correct 4xx statuses, or it will turn a 405 into a 500 again.
If we really need a resolver that handles some exceptions itself, extending AbstractHandlerExceptionResolver is convenient: it has setOrder, it can be limited to certain handlers with setMappedHandlerClasses, and its doResolveException returns null for anything we do not recognise. To put it inside the default list rather than around it, we implement WebMvcConfigurer.extendHandlerExceptionResolvers and add it at a chosen index, for example right after ExceptionHandlerExceptionResolver.
5Locking the Behaviour In With Tests
Ordering bugs come back whenever someone adds a resolver or an advice, so we write one MockMvc test per exception family. Each asserts the status and the body shape, which documents the intended order better than any comment.
Notice that MockMvc runs the real DispatcherServlet with the real resolver chain, so these tests catch ordering mistakes. It does not perform the container's error dispatch to /error, so for filter exceptions we need a test against a running server.
6Follow-up questions
Why not just use HIGHEST_PRECEDENCE and call the defaults myself?
Because we would be rebuilding Spring's logic by hand and would have to keep it in step with every release. Returning null for exceptions we do not own is simpler and keeps the advice and the default status mapping working. A resolver that runs first should observe, not decide.
How do I log exceptions that happen in filters or Spring Security?
Resolvers never see them, because they happen before or after the DispatcherServlet. We log them in a filter that wraps the chain, in Spring Security's AuthenticationEntryPoint and AccessDeniedHandler, or in the /error handling, where the exception is available from ErrorAttributes.
Does the order of @ControllerAdvice classes matter too?
Yes. When several advice classes can handle the same exception, ExceptionHandlerExceptionResolver tries them in their order, which we set with @Order or Ordered. Within one class, the closest matching exception type wins. A catch-all advice should therefore have the lowest precedence so specific advice classes are tried first.
7Conclusion
In this answer, we saw that the DispatcherServlet asks exception resolvers in order and stops at the first ModelAndView. A custom resolver with high precedence takes over every exception and hides @ExceptionHandler methods and the 4xx mapping of standard exceptions, while one with no order runs after the defaults and only sees leftovers. The fix is to give each resolver a deliberate order and return null for what it does not own: a logging resolver runs first and always passes, and the JSON fallback moves into an advice with ResponseEntityExceptionHandler. MockMvc tests per exception type keep the order from drifting.
Open with
Exception resolvers are sorted by order and the first one that returns a ModelAndView, even an empty one, ends the search. Spring's three defaults sit in a composite with order 0.
Then, if they want more
If our custom resolver has Ordered.HIGHEST_PRECEDENCE, it runs first, and because it returns a ModelAndView for every exception, the advice's @ExceptionHandler methods never run, and even a 405 or 415 turns into our 500.
If it has no order, it gets lowest precedence, runs after the composite, and only sees exceptions nobody handled, so for everything with an @ExceptionHandler, @ResponseStatus or a standard Spring MVC mapping it is never called. And it never sees exceptions from filters or Spring Security at all. To fix it, I decide what the resolver is for.
For logging, I keep it early but make it return null, so it observes and the chain continues. For a JSON fallback, I move it into a @RestControllerAdvice with an @ExceptionHandler for Exception that extends ResponseEntityExceptionHandler, so specific handlers and standard statuses still win.
If I need a real resolver, I extend AbstractHandlerExceptionResolver with setOrder, or insert it at a known position with WebMvcConfigurer.extendHandlerExceptionResolvers. Then I add MockMvc tests for a domain exception, a 405 and an unexpected exception, so the ordering cannot silently change again.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A custom HandlerExceptionResolver that logs and returns JSON for all exceptions was...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/cd06e4844b/