Global Exception Handling with @ControllerAdvice and @ExceptionHandler
Fast recall
Quick revision
Core answer
@ControllerAdvice creates a global exception handler that applies to all controllers. @ExceptionHandler methods within it catch specific exception types and return structured error responses. This eliminates scattered try-catch blocks in controllers and ensures consistent error response format across the API. Spring Boot 3.x provides `ProblemDetail` (RFC 7807) as a standard error response structure with type, title, status, detail, and instance fields. Use @RestControllerAdvice for REST APIs (it adds @ResponseBody automatically), and never catch `Exception.class` as the only handler — it hides genuine bugs.Whether the candidate understands the difference between local `@ExceptionHandler` and global `@ControllerAdvice`, and can design a consistent error response
Catching `Exception.class` too broadly in `@ControllerAdvice`, accidentally hiding real bugs by returning `400 Bad Request` for server errors that should be `500`
Mention RFC 7807 `ProblemDetail` support in Spring Boot 3.x, `spring.mvc.problemdetails.enabled=true`, and handler method resolution order when multiple handlers match
Interview-ready guide
Interview answer
Centralized exception handling means one place decides what every error response looks like — consistent HTTP status codes, a standard error body, and no stack traces leaking to clients.
`@ControllerAdvice` vs local `@ExceptionHandler`
@ExceptionHandlerinside a@Controllerapplies only to exceptions from that controller@ControllerAdvice(or@RestControllerAdvice) applies to all controllers — one class handles every unhandled exception- When both exist, the local handler takes precedence for its controller
Handler priority
- Spring picks the
@ExceptionHandlerwith the most specific matching exception type ConstraintViolationExceptionhandler wins overExceptionhandler for validation failures- Never catch
Exception.classas the only handler — you accidentally suppress programming errors
Spring Boot 3.x `ProblemDetail` (RFC 7807)
- Set
spring.mvc.problemdetails.enabled=trueand Spring Boot returns a standardized JSON error body for built-in exceptions - Custom handlers that return
ProblemDetailproduce the same shape — consistent API contract - Clients can rely on a predictable error format without parsing free-form messages
Validation handlers
MethodArgumentNotValidException— fires for@Valid @RequestBodyfailuresConstraintViolationException— fires for@Validatedon path/query parameters- Extract field-level errors and add them as a
propertiesmap onProblemDetail
HTTP status code discipline
400for validation failures,404for missing resources,409for conflicts,422for business rule violations,500for unexpected errors- Never return
200with an error payload
In an interview, the strong answer is: `@RestControllerAdvice` + `ProblemDetail` + specific handlers per exception type, with no catch-all that downgrades server errors to 400.
Global Exception Handling with @ControllerAdvice and @ExceptionHandler
Direct Answer
@ControllerAdvice creates a global exception handler that applies to all controllers. @ExceptionHandler methods within it catch specific exception types and return structured error responses. This eliminates scattered try-catch blocks in controllers and ensures consistent error response format across the API. Spring Boot 3.x provides ProblemDetail (RFC 7807) as a standard error response structure with type, title, status, detail, and instance fields. Use @RestControllerAdvice for REST APIs (it adds @ResponseBody automatically), and never catch Exception.class as the only handler — it hides genuine bugs.
Common Mistakes & Pitfalls
Catching Exception.class too broadly in @ControllerAdvice, accidentally hiding real bugs by returning 400 Bad Request for server errors that should be 500
Check your understanding
Answer these without looking back at the article.
- What is the difference between `@ControllerAdvice` and `@RestControllerAdvice`?
- How do you handle both `MethodArgumentNotValidException` and `ConstraintViolationException` in the same advice?
- How does Spring resolve which `@ExceptionHandler` to call when multiple handlers match?
- How would you add a `traceId` field from MDC to every error response automatically?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.