Exception handling in Spring Boot is used to manage application errors in a centralized and consistent manner. Instead of handling exceptions in every controller method, Spring Boot provides mechanisms such as @ExceptionHandler, @ControllerAdvice, and @RestControllerAdvice to catch exceptions globally and return meaningful error responses to clients.
Why Do We Need Exception Handling?
Good exception handling ensures predictable and user-friendly error responses.
Common Exception Handling Approaches
1. Using @ExceptionHandler
This annotation handles specific exceptions within a controller.
@RestController
public class EmployeeController {
@ExceptionHandler(
EmployeeNotFoundException.class)
public String handleException(
EmployeeNotFoundException ex) {
return ex.getMessage();
}
}Whenever EmployeeNotFoundException occurs, this method is executed.
2. Using Global Exception Handling
For enterprise applications, global exception handling is preferred.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(
EmployeeNotFoundException.class)
public ResponseEntity<String>
handleEmployeeException(
EmployeeNotFoundException ex) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());
}
}This handles exceptions across all controllers.
Why Use @RestControllerAdvice?
Instead of writing exception handlers in every controller, all exception logic is placed in one class.
Handling Multiple Exceptions
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(
NullPointerException.class)
public ResponseEntity<String>
handleNullPointerException(
NullPointerException ex) {
return ResponseEntity
.badRequest()
.body("Null value encountered");
}
@ExceptionHandler(
Exception.class)
public ResponseEntity<String>
handleGenericException(
Exception ex) {
return ResponseEntity
.status(
HttpStatus.INTERNAL_SERVER_ERROR)
.body("Something went wrong");
}
}Different exceptions can return different responses.
Custom Error Response
Instead of returning plain text, APIs usually return structured JSON.
public class ErrorResponse {
private String message;
private int status;
public ErrorResponse(
String message,
int status) {
this.message = message;
this.status = status;
}
}@ExceptionHandler(
EmployeeNotFoundException.class)
public ResponseEntity<ErrorResponse>
handleException(
EmployeeNotFoundException ex) {
ErrorResponse response =
new ErrorResponse(
ex.getMessage(),
404);
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(response);
}{
"message": "Employee not found",
"status": 404
}
Handling Validation Errors
@Valid
for request validation.
@PostMapping
public Employee createEmployee(
@Valid
@RequestBody Employee employee) {
return employee;
}Validation failures can also be handled globally using @ExceptionHandler.
How Exception Handling Works
Client Request
|
Controller
|
Exception Occurs
|
Exception Handler
|
Error Response Generated
|
Client Receives Response
This keeps the application stable and prevents unexpected crashes.
GET /employees/101
EmployeeNotFoundException
is thrown.
{
"message": "Employee not found",
"status": 404
}
Instead of an internal server error.
Benefits
Real-World Example
A global exception handler can catch all these exceptions and return standardized JSON responses, making the API easier for frontend and mobile applications to consume.
A concise interview answer is
In Spring Boot, exceptions are commonly handled using @ExceptionHandler along with @ControllerAdvice or @RestControllerAdvice. This allows centralized exception handling across the application and helps return consistent, meaningful error responses with appropriate HTTP status codes.
Last updated: 11 November 2026
Previous question: What is the role of the @SpringBootTest annotation?
Next question: Explain the purpose of the pom.xml file in a Maven project.