Spring Exception Handling

Implements standardized exception handling in Spring Boot with GlobalExceptionHandler and custom exception hierarchies.

Updated May 12, 2026
One-click install
npx skills add https://github.com/ZzZueszZ/claude-kit --skill spring-exception-handling-zzzueszz
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: Spring Exception Handling
Source: https://github.com/ZzZueszZ/claude-kit/tree/main/.claude/skills/spring-exception-handling
Command: npx skills add https://github.com/ZzZueszZ/claude-kit --skill spring-exception-handling-zzzueszz

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Spring Boot applications often suffer from inconsistent error handling: generic RuntimeExceptions, swallowed exceptions, HTTP 200 responses for errors, and leaked stack traces. This Skill provides a complete, standardized pattern for exception handling so every API returns consistent, secure error responses. ## Core Features & Use Cases - Custom Exception Hierarchy: Defines BaseException split into BusinessException and TechnicalException branches, with concrete exceptions like ResourceNotFoundException, DuplicateResourceException, and InvalidOperationException carrying error codes and HTTP statuses. - GlobalExceptionHandler Template: A @RestControllerAdvice handling custom exceptions, validation errors (@Valid), constraint violations, type mismatches, access denied, and a catch-all for unexpected errors. - RFC 7807-inspired ErrorResponse: A consistent JSON error format with status, errorCode, message, path, timestamp, and field-level validation errors. - Use Case: When building a Spring Boot REST API, apply this Skill to generate the exception hierarchy, global handler, and error response DTO so all endpoints return uniform error payloads without exposing internal details. ## Quick Start Ask the AI to set up standardized exception handling for your Spring Boot project following the spring-exception-handling skill, including the exception hierarchy and GlobalExceptionHandler.

Frequently Asked Questions about Spring Exception Handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a global exception handler in Spring Boot?▼

Create a class annotated with @RestControllerAdvice and define @ExceptionHandler methods for each exception type. Return a consistent ErrorResponse body with status, errorCode, message, path, and timestamp for every handled exception.

How to handle validation errors from @Valid in Spring Boot?▼

Handle MethodArgumentNotValidException in your @RestControllerAdvice and extract FieldError entries from the BindingResult. Map each field name to its validation message and return them in a fieldErrors object with HTTP 400.

Should I use custom exceptions or RuntimeException in Spring services?▼

Use a custom exception hierarchy instead of generic RuntimeException. Define an abstract BaseException carrying an errorCode and HttpStatus, then extend it with specific exceptions like ResourceNotFoundException so handlers can map them precisely.

Why should Spring Boot APIs not expose stack traces in error responses?▼

Exposing stack traces leaks internal implementation details and creates a security risk. Log the full exception server-side with log.error, but return only a generic message like "An unexpected error occurred" with HTTP 500 to clients.

What HTTP status codes should business exceptions return?▼

Map each business exception to a semantically correct status: 404 for resource not found, 409 for duplicate resources, 422 for invalid operations, and 403 for access denied. Never return HTTP 200 with an error payload in the body.