> For the complete documentation index, see [llms.txt](https://dev117uday.gitbook.io/notes-md/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev117uday.gitbook.io/notes-md/spring-boot/spring-boot-controller/response-entity-exception-handling.md).

# Response Entity Exception Handling

* mark the class with `ControllerAdvice` and `ResponseStatus`
* The class should **extend** `ResponseEntityExceptionHandler`
* Annotate the member function with `@ExceptionHandler(T.class)` where T is the custom exception class
* Make sure to include `String message` and `Integer statusCode` in the custom exception.
* Example

```java
@ControllerAdvice
@ResponseStatus
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler 
{

    @ExceptionHandler(DepartmentExceptions.class)
    public ResponseEntity<ErrorMessage> 
        departmentException(DepartmentExceptions exception) 
    {

        Integer exceptionStatus = exception.getStatusCode();

        if (exceptionStatus == 404) {
            ErrorMessage message =
                new ErrorMessage(HttpStatus.NOT_FOUND, exception.getMessage());
            
            return ResponseEntity.status(HttpStatus.CREATED).body(message);
        }
    }

}
```
