Spring Exception Handling

 Spring Boot Exception Handling


Custom Exception Class

These classes enable us to customize an exception according to the callers’ needs.

ApiRequestException: Is thrown when tries to add an already existing admin email or nic.








Global Exception Handling Using @ControllerAdvice 


A controller advice enables you to apply the same exception handling procedures across the entire program, rather than simply to a single controller. They can be 
compared to an annotation-driven interceptor.



1. First we have to create a payload containing exception details.
2. Then return the response entity.




@ExceptionHandler is a Spring annotation that provides a way for dealing with exceptions thrown during handler execution (Controller operations). This annotation will serve as the entry point for handling exceptions thrown within this controller only if it is used on methods of controller classes.





The class that will represent the actual exception




message : the error message associated with the exception
httpStatus : the HTTP status code
timestamp : store the instant on the timeline up to a precision of nanoseconds









Post a Comment

0 Comments