@ExceptionHandler can be used locally or globally. The local level will mean the use of this annotation inside the controller itself to handle exceptions only in this controller. All errors caused by this controller will be detected by this @ExceptionHandler. But this will mean that if there is a similar exception in the other controller, you will have to rewrite the corresponding code in this controller locally again.
To prevent this exception handling style from repeating to the controller, we can write @ExceptionHanlder globally with another annotation called @ControllerAdvice.
@ControllerAdvice does not apply to exception handling, and is also used to handle property bindings, validation, or formatting globally. @ControllerAdvice in the context of exception handling is another way to handle exceptions globally using the @Exceptionhandler annotation.
Now moving to HandlerExceptionResolver is a lower level interface. Spring provides 2 implementations of this:
- ResponseStatusExceptionResolver: supports @ResponseStatus annotation support
- ExceptionHandlerExceptionResolver: supports @ExceptionHandler annotation
Example. Therefore, when you want to handle exceptions and choose an exception handling strategy, you need to think about choosing between using local or global exception handling using annotations. How do you need to provide HTTP status codes, how to wrap it in an @Response object, etc., How do you want to redirect to handler pages, transfer data using flash attributes or get parameters, etc. Etc. Or perhaps skip annotations and use SimpleMappingExceptionResolver and start mapping specific exceptions to the URLs of the error handler page
Here we will not consider the lower level that underlies the HandlerExceptionResolver at this stage, since we are dealing with its implementation at a higher level and create a strategy based on these parameters.
In the context above, to respond to your request - @ControllerAdvice was not introduced to handle exceptions, it is a mechanism that you can use to handle exceptions globally with @ExceptionHandler. HandlerExceptionResolver is an interface whose implementation helps support @ResponseStatus and @Exceptionhandler annotations. Unless you want to handle MVC-related exceptions, as the Spring framework does not provide proper exception handling. Therefore, if you need to pass e problems related to incorrect @Requestmapping, etc., which would not be caught by the controller, since it would not even reach it in the 1st place, then it would be useful to implement HandlerExceptionResolver