0
0
Spring Bootframework~5 mins

Why centralized error handling matters in Spring Boot

Choose your learning style9 modes available
Introduction

Centralized error handling helps catch and manage errors in one place. This makes your app easier to fix and keeps it running smoothly.

When you want to show friendly error messages to users instead of technical details.
When you need to log all errors consistently for later review.
When you want to avoid repeating error handling code in many places.
When you want to handle different error types with specific responses.
When you want to keep your code clean and easier to maintain.
Syntax
Spring Boot
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        // handle error and return response
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
    }
}
Use @ControllerAdvice to mark a class for global error handling.
Use @ExceptionHandler to specify which errors to catch.
Examples
This example catches only null pointer errors and returns a 400 status with a message.
Spring Boot
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<String> handleNullPointer(NullPointerException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null value found");
    }
}
This example catches all exceptions and returns a generic error message with status 500.
Spring Boot
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
    }
}
Sample Program

This global handler catches illegal argument errors and returns a 400 status with a message. It also catches any other exceptions and returns a 500 status with a generic message.

Spring Boot
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid argument: " + ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal server error");
    }
}
OutputSuccess
Important Notes

Centralized error handling improves user experience by showing clear messages.

It helps developers find and fix bugs faster by logging errors in one place.

Remember to handle specific exceptions before the general Exception class to avoid masking errors.

Summary

Centralized error handling catches errors in one place for easier management.

It helps show friendly messages and keeps code clean.

Use @ControllerAdvice and @ExceptionHandler in Spring Boot for this.