@ExceptionHandler and @ControllerAdvice for Centralised Error Handling
Fast recall
Quick revision
Core answer
Ability to design clean, maintainable error handling in a real Spring application rather than scattering try-catch in every controller.
Defining @ExceptionHandler only inside a controller class (local scope) when the requirement is global; also returning HTML error pages from a @RestControllerAdvice instead of a structured JSON error body.
Discuss the difference between @ControllerAdvice and @RestControllerAdvice, mention that you can scope advice to specific packages or base classes, and show how to map exceptions to RFC 7807 ProblemDetail (Spring 6+).
Interview-ready guide
Interview answer
Exception handling in Spring MVC has two levels: local handlers inside a controller, and global handlers in @ControllerAdvice. Getting the scoping and precedence right is what determines which handler fires for which exception.
`@ControllerAdvice` scope
- By default, applies to all controllers in the application
- Narrow the scope:
@ControllerAdvice(assignableTypes = OrderController.class)or@ControllerAdvice(basePackages = "com.example.orders") @RestControllerAdvice=@ControllerAdvice+@ResponseBody— the return value of every@ExceptionHandlermethod is written as the response body viaHttpMessageConverter
Handler resolution
- Spring walks the exception class hierarchy to find the most specific matching
@ExceptionHandler - Local handler in the originating controller takes precedence over global
@ControllerAdvice - Multiple
@ControllerAdvicebeans: order them with@Order; lower value = higher priority
Extending `ResponseEntityExceptionHandler`
- Extend it in your
@RestControllerAdviceto get free handling of Spring MVC built-in exceptions:MethodArgumentNotValidException(Bean Validation failures),NoHandlerFoundException(404),HttpMessageNotReadableException(malformed JSON) - Override the protected methods to customize the response body format
ProblemDetail (Spring Boot 3.x)
spring.mvc.problemdetails.enabled=trueswitches Spring's default error responses to RFC 7807 format automatically- Return
ProblemDetailfrom your@ExceptionHandlermethods for a standardized, interoperable error format
@ExceptionHandler and @ControllerAdvice for Centralised Error Handling
Direct Answer
@ExceptionHandler marks a method that handles a specific exception type; when placed inside a @ControllerAdvice class, it applies globally across all controllers in the application, enabling a single, reusable error-handling strategy rather than duplicating catch blocks in every controller.
Common Mistakes & Pitfalls
Defining @ExceptionHandler only inside a controller class (local scope) when the requirement is global; also returning HTML error pages from a @RestControllerAdvice instead of a structured JSON error body.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @ControllerAdvice and @RestControllerAdvice?
- How would you return RFC 7807 ProblemDetail responses in Spring 6?
- Can two @ControllerAdvice beans handle the same exception? Which one wins?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.