Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springhard

How do you handle exceptions globally in Spring MVC?

Tags
#controlleradvice#exceptionhandler#error-handling
Back to categoryPractice quiz

Answer

Use `@ControllerAdvice` with `@ExceptionHandler` to map exceptions to consistent HTTP responses (status + body). It keeps controllers clean and centralizes error handling.

@ControllerAdvice
class ApiErrors {
  @ExceptionHandler(IllegalArgumentException.class)
  ResponseEntity<String> badRequest(IllegalArgumentException e) {
    return ResponseEntity.badRequest().body(e.getMessage());
  }
}

Related questions

Java
Checked vs Unchecked Exceptions?
#exception#error-handling#java