Backend Development 20 min read

Unified Exception Handling in Spring Boot Using @ControllerAdvice, Assert, and Enum‑Based Error Codes

This article explains how to replace repetitive try‑catch blocks in Java Spring applications with a unified exception handling strategy that leverages @ControllerAdvice, custom Assert utilities, and enum‑driven error codes to produce clean, maintainable backend code and consistent error responses.

Top Architect
Top Architect
Top Architect
Unified Exception Handling in Spring Boot Using @ControllerAdvice, Assert, and Enum‑Based Error Codes

In large Java Spring projects, developers often spend half of their time writing repetitive try { … } catch { … } blocks, which makes the code noisy and hard to read. The article proposes a clean alternative by using Spring's @ControllerAdvice together with a custom Assert utility and an enum that defines error codes and messages.

The Assert interface provides default methods such as assertNotNull(Object obj) that internally throw a business exception defined by an enum instance. Example:

public interface Assert {
    default void assertNotNull(Object obj) {
        if (obj == null) {
            throw newException();
        }
    }
    BaseException newException(Object... args);
}

An enum implementing BusinessExceptionAssert maps each error scenario to a specific code and message, e.g., BAD_LICENCE_TYPE(7001, "Bad licence type.") . Controllers can then validate inputs with a single line:

ResponseEnum.BAD_LICENCE_TYPE.assertNotNull(licenceTypeEnum);
ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);

The unified exception handler, annotated with @ControllerAdvice , catches all exceptions, distinguishes between business, servlet, and unknown errors, and returns a standardized JSON response containing code and message . It also supports internationalization by looking up messages from a message source.

Configuration snippets show how to force Spring to throw a NoHandlerFoundException for 404 errors, enabling them to be handled uniformly. The article demonstrates the approach with a sample LicenceService that uses MyBatis‑Plus for data access, and provides screenshots of the resulting API responses for various error scenarios.

Overall, the pattern eliminates the need for numerous custom exception classes, improves readability, and ensures consistent error handling across the entire backend service.

backendJavaException HandlingSpringenumAssertUnified Error Handling
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.