@ExceptionHandler
The Spring annotation that marks a method to handle a particular exception type thrown by controllers and turn it into a response.
In more detail
Placed in controller advice, it applies to every controller. The course handler turns an OrderMissing exception into a 404 problem detail.
Example
@ExceptionHandler(OrderMissing.class) ProblemDetail missing(OrderMissing error)Related terms
- controller advice — A Spring class, marked @RestControllerAdvice, whose exception handlers apply across all controllers, giving one failure policy for every route.
- Problem Details — A standard JSON shape for HTTP error responses, defined in RFC 9457, with members such as type, title, status, detail and instance.
- domain exception — An exception named for a business situation, such as OrderMissing or StorageFailure, rather than for a technical cause.
Taught in
- Exception Handling To Problem Details — Modern Java: Virtual Threads & High-Throughput Services