Quick revision
The short answer
DispatcherServlet passes the exception to its HandlerExceptionResolvers in order. First ExceptionHandlerExceptionResolver looks for an @ExceptionHandler in the controller and then in the @ControllerAdvice beans, then ResponseStatusExceptionResolver handles @ResponseStatus and ResponseStatusException, then DefaultHandlerExceptionResolver maps Spring's own exceptions to 4xx and 5xx codes. If none of them resolves it, the exception propagates to the servlet container, which forwards to /error, where Spring Boot's BasicErrorController builds a 500 response from ErrorAttributes.
What to remember
In this answer, we followed an exception out of a controller. DispatcherServlet asks its resolvers in order: ExceptionHandlerExceptionResolver uses @ExceptionHandler methods from the controller first and then from advice beans in their order, ResponseStatusExceptionResolver handles @ResponseStatus and ResponseStatusException, and DefaultHandlerExceptionResolver maps Spring's standard exceptions. If none resolves it, the container forwards to /error, where BasicErrorController builds a 500 response from ErrorAttributes, and the same page renders the bodies for the two resolvers that call sendError.
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.
DispatcherServlet passes the exception to its HandlerExceptionResolvers in order. First ExceptionHandlerExceptionResolver looks for an @ExceptionHandler in the controller and then in the @ControllerAdvice beans, then ResponseStatusExceptionResolver handles @ResponseStatus and ResponseStatusException, then DefaultHandlerExceptionResolver maps Spring's own exceptions to 4xx and 5xx codes. If none of them resolves it, the exception propagates to the servlet container, which forwards to /error, where Spring Boot's BasicErrorController builds a 500 response from ErrorAttributes.
Why this question is asked
When an error response looks wrong, an HTML page where we expected JSON, a 500 where we expected a 400, a body in a format nobody recognises, the cause is almost always that the exception was handled by a different step than we thought. Knowing the order of the chain lets us predict which code produced a response and where to change it. Interviewers ask this internals question to see whether we can debug error handling instead of adding handlers until it looks right. In this answer, we will recap what a handler exception resolver is, walk through the three resolvers in order, see what happens when none of them resolves the exception, and look at what /error returns.
1A Quick Recap of HandlerExceptionResolver
DispatcherServlet is the front controller of Spring MVC: every request goes through it, and it calls our controller method. When that method throws, DispatcherServlet does not let the exception escape at once. It asks a list of HandlerExceptionResolver beans, one by one, whether they can deal with it. A resolver either handles the exception, by writing a response or returning a ModelAndView, or returns null to say it is not responsible, and the next one is asked.
In a standard setup the list contains three resolvers, wrapped in a composite in a fixed order. Spring Boot also places its DefaultErrorAttributes first in the list. It only records the exception as a request attribute so that the error page can find it later, and then returns null, so it never resolves anything itself.
- Controller method throwsfor example
OrderNotFoundException - @ExceptionHandler in the controller or an advice?YesExceptionHandlerExceptionResolver writes the responseNonext resolver
- @ResponseStatus or ResponseStatusException?YesResponseStatusExceptionResolver calls sendErrorNonext resolver
- Standard Spring MVC exception?YesDefaultHandlerExceptionResolver calls sendErrorNounresolved
- Exception reaches the servlet containerwrapped and rethrown by
DispatcherServlet - Container forwards to /error
BasicErrorControllerusesErrorAttributes, status 500
6 steps in order. Play them, or step through them one at a time.
2ExceptionHandlerExceptionResolver
This resolver handles @ExceptionHandler methods, and it searches in a fixed order. It looks first in the controller class that threw, so a local handler always wins over a global one. If that class has no matching handler, it goes through the @ControllerAdvice and @RestControllerAdvice beans in their order, set with @Order or the Ordered interface, and uses the first advice that has any matching handler. Inside one class, the handler with the closest exception type wins.
When a handler is found, the resolver invokes it like a normal controller method, so a returned ProblemDetail or ResponseEntity is written as JSON with the chosen status. This is the only resolver that gives us full control of the body, which is why most applications want their exceptions handled here. In Spring Boot, enabling spring.mvc.problemdetails.enabled registers an advice based on ResponseEntityExceptionHandler, so even the standard Spring exceptions are resolved in this first step and get ProblemDetail bodies.
3ResponseStatusExceptionResolver
If no handler method matched, this resolver checks two things. Is the exception a ResponseStatusException, the exception type that carries its own status, or does the exception class, or one of its causes, carry @ResponseStatus? If so, it calls response.sendError with that status and reason.
Notice what sendError means. It does not write a body itself. It marks the response as an error and lets the servlet container render the error page for that status, which in Spring Boot means the request is forwarded to /error. So the status is ours, but the body comes from BasicErrorController.
4DefaultHandlerExceptionResolver
The last resolver knows Spring MVC's own exceptions and maps each to the status the HTTP specification suggests. A wrong HTTP method gives HttpRequestMethodNotSupportedException and 405, an unsupported Content-Type gives 415, a missing required parameter or unreadable JSON gives 400, a missing static resource gives 404, and an async request timeout gives 503. Like step 2, it calls sendError, so the body again comes from the error page.
| Exception | Status |
|---|---|
HttpRequestMethodNotSupportedException | 405 |
HttpMediaTypeNotSupportedException | 415 |
MissingServletRequestParameterException | 400 |
HttpMessageNotReadableException (bad JSON) | 400 |
MethodArgumentNotValidException | 400 |
NoResourceFoundException | 404 |
AsyncRequestTimeoutException | 503 |
5When Nothing Resolves It: the Container and /error
If all three resolvers return null, DispatcherServlet rethrows the exception, wrapped in a ServletException if it is not already one, and it travels up through any filters to the servlet container, such as Tomcat. The container looks for an error page mapping. Spring Boot registers a global error page at /error, configurable with server.error.path, so the container performs an internal ERROR dispatch to that path with the status 500 and the exception stored in request attributes.
/error is handled by Spring Boot's BasicErrorController. It has two methods: one for clients that accept text/html, which renders the Whitelabel error page or a template such as error/500.html, and one for everyone else, which returns JSON. Both get their content from the ErrorAttributes bean, by default DefaultErrorAttributes.
So an unresolved exception always becomes a 500 with Spring Boot's own JSON shape, not our API's format. The same /error path also renders the bodies for steps 2 and 3, which call sendError, with the status they chose instead of 500. To change what /error returns we customise ErrorAttributes or replace the ErrorController, but the better fix is usually to resolve more exceptions in step 1.
6Follow-up questions
Why does a @ResponseStatus exception not use my ProblemDetail format?
Because it is resolved in step 2, which calls sendError and lets /error render the body with ErrorAttributes. Our @ControllerAdvice is only used in step 1. To get our format, we either add an @ExceptionHandler for that exception or a handler for ResponseStatusException, or we make /error produce the same shape.
Do exceptions thrown in a servlet filter go through these resolvers?
No. The resolvers are called by DispatcherServlet for exceptions from handler execution. A filter runs before DispatcherServlet, so its exceptions go straight to the container and then to /error. That is why errors from filters, including many security errors, often look different.
Can we add our own HandlerExceptionResolver?
Yes, by implementing HandlerExceptionResolver and Ordered and registering it as a bean or through WebMvcConfigurer.extendHandlerExceptionResolvers. It is rarely needed, because @ExceptionHandler covers almost every case, but it is useful for cross-cutting behaviour such as recording metrics for every exception before the normal resolvers run.
7Conclusion
In this answer, we followed an exception out of a controller. DispatcherServlet asks its resolvers in order: ExceptionHandlerExceptionResolver uses @ExceptionHandler methods from the controller first and then from advice beans in their order, ResponseStatusExceptionResolver handles @ResponseStatus and ResponseStatusException, and DefaultHandlerExceptionResolver maps Spring's standard exceptions. If none resolves it, the container forwards to /error, where BasicErrorController builds a 500 response from ErrorAttributes, and the same page renders the bodies for the two resolvers that call sendError.
Open with
When a controller throws, DispatcherServlet asks its HandlerExceptionResolvers in order. Boot puts DefaultErrorAttributes first, but it only stores the exception for later and returns null. The first real one is ExceptionHandlerExceptionResolver.
Then, if they want more
It looks for an @ExceptionHandler in the controller itself, then in the @ControllerAdvice beans in their @Order, and within a class the closest exception type wins. If it finds one, the handler writes the response, which is the only place I fully control the body. Next is ResponseStatusExceptionResolver, for exceptions annotated with @ResponseStatus or a ResponseStatusException.
Then DefaultHandlerExceptionResolver, which maps Spring's own exceptions, like 405 for a wrong method, 415 for a bad content type and 400 for unreadable JSON. Those two call response.sendError, so the status is set but the body comes from the error page.
If nothing resolves it, DispatcherServlet rethrows, the exception goes to the container, and Boot's error page mapping forwards to /error. BasicErrorController handles that, returning the Whitelabel page for browsers or JSON for others, built from ErrorAttributes: timestamp, status 500, error and path, with message and trace only if server.error.include properties allow.
So if I want one format, I make sure exceptions are resolved in the first step.
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 "In what order does Spring MVC try to handle an exception from a controller, and what...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/598e57bb1c/