Billing System using Spring Boot -Integrating Centralized Exception handling

Centralized exception handling is a great feature in Spring Boot. Once we created a common method for a particular exception we can reuse that exception handler method anywhere in your application. The following three things are important to implement centralized exception handling.

1) ResponseEntityExceptionHandler
2) @ControllerAdvice

The above two friends will help you to implement the centralized exception handler.

To implement our generic Exception handling method, we need to create a new class. This must extend from ResponseEntityExceptionHandler.

@ControllerAdvice
public class BillingExceptionHandler extends ResponseEntityExceptionHandler {

Syntax to create a generic exception handler class
@ExceptionHandler(Your Exception handler class)
When you create an exception handling method, the method must be annotated with the above annotation and you need to pass your Exception class as a parameter which you want to handle. For example. if you want to handle the “InvalidInputException” we have to define our method like below.

@ExceptionHandler(InvalidInputException.class)
public final ResponseEntity<Object> invalidInputException(InvalidInputException ex, WebRequest request) throws Exception {
Your method body
}

For further reading please click the below link.
Spring boot centralized exception handling step by step instruction