Global Exceptional Handling in Spring and Hibernate using @ControllerAdvice and @ExceptionHandler

Hello guys, Bushan here, welcome back to B2 Tech. If you’are creating any kind of application handling exceptions is the most important part because it allows us to run the application smoothly without crashing anywhere. Today in this article, let’s discuss how to handle Exceptions in Spring and Hibernate using @ControllerAdvice and @ExceptionHandler.

@ControllerAdvice used for global error handling in the Spring MVC application. It also has full control over the body of the response and the status code.

Using @ControllerAdvice you can have a centralized way to handle exceptions, binding, etc. it applies to all the defined controller.

@ExceptionHandler works at the Controller level and it is only active for that particular Controller, not globally for the entire application.

@ExceptionHandler is local to a controller: only exceptions from this controller is routed to his @ExceptionHandler

You don’t handle exceptions at all levels of your application simultaneously; you have to consider their contextual meaning and what the appropriate strategy is for your application. Some errors should be ignored, some should be wrapped, some should be allowed to be raised directly.

One method for dealing with exceptions in a Spring-MVC application is to wrap fatal errors from the underlying libraries with your own where appropriate, named for the level at which they are thrown, e.g. ServiceException or RepositoryException. A @ControllerAdvice-annotated class can then handle these errors with @ErrorHandler-annotated methods and return 5XX HTTP errors.

Common application errors, like an entity not being found due to an incorrect id, can result in a custom exception, e.g. NotFoundException being raised and subsequently caught in your @ControllerAdvice-annotated class.

The advantage to this technique is that you have less error-handling code in your different application layers and can centralize the conversion of exceptions to responses.

Here in this article, we are going to handle 3 types of Exceptions, if the user types a wrong URL or if the request mapping is not found then we will handle the 404/PageNotFoundException. Next, if the entity is not found or the id is not found then we will handle IdNotFoundException and finally, we are going to handle a Generalized exception.

Let’s create a maven web app project. So here is my project structure











Next, create a Database

Next, create a Table


Add the below code in web.xml for handling exceptions

Create a class called GlobalDefaultExceptionHandler and annotate this class with @ControllerAdvice

Inside this class write a method and annotate this method with @ExceptionHandler and pass NoHandlerFoundException.class as a parameter.

Inside the same class, write one more method to handle customized exception which is EntityNotFoundException/IdNotFoundException/CountryNotFoundException and we will annotate this method with @ExceptionHandler but this time we will pass our customized exception class CountryNotFoundException.class as a parameter.

Inside the controller, while retrieving the Country, check for the NULL, if it is NULL then throw CountryNotFoundException

To handle the generalized exception, inside the same class we will write one more method and will annotate this method with @ExceptionHandler and this time we will pass Exception.class as a parameter.

To display the error messages to the user we will create a view page called error.jsp and will display the error messages.

Here is the complete source code for the example.





Here is the quick demo of the application,

That’s it for this article, I hope this article is helped you in one or the other way, if so please share this article with your social media profiles. If you have any queries regarding this article then leave your questions in the comment section I will try to answer it. If you are looking for source code of this example, then I give a link to the source code at the end of this article. Thank you so much of reading, I will see you in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *