0
0
Spring Bootframework~10 mins

@ControllerAdvice for global handling in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @ControllerAdvice for global handling
Start: HTTP Request
Controller Method
Exception Thrown?
NoReturn Normal Response
Yes
@ControllerAdvice Handler
Handle Exception Globally
Return Error Response
When a controller throws an exception, @ControllerAdvice catches it globally and returns a custom response.
Execution Sample
Spring Boot
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
  @ExceptionHandler(Exception.class)
  public String handleAllExceptions(Exception ex) {
    return "error-page";
  }
}
This code catches all exceptions from any controller and returns an error page.
Execution Table
StepActionException Thrown?Handler InvokedResponse Returned
1HTTP request arrives at controllerNoNoController normal response
2Controller method executesYes (e.g. NullPointerException)Yes (@ControllerAdvice method)Custom error page
3Exception handled by global handlerHandledYesError response sent to client
💡 Exception handled globally by @ControllerAdvice, normal flow interrupted to return error response
Variable Tracker
VariableStartAfter Step 1After Step 2Final
exceptionThrownfalsefalsetruetrue
handlerInvokedfalsefalsetruetrue
responsenullnormal responseerror pageerror page
Key Moments - 2 Insights
Why does the normal controller response not get sent when an exception occurs?
Because the exception interrupts normal flow and @ControllerAdvice catches it to send a custom error response instead, as shown in execution_table step 2.
Does @ControllerAdvice handle exceptions from all controllers automatically?
Yes, @ControllerAdvice applies globally to all controllers unless limited by annotations or configuration, as seen by handlerInvoked being true whenever an exception occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response returned at step 1?
ACustom error page
BController normal response
CNo response yet
DException message
💡 Hint
Check the 'Response Returned' column at step 1 in the execution_table
At which step does the @ControllerAdvice handler get invoked?
AStep 3
BStep 1
CStep 2
DNever
💡 Hint
Look at the 'Handler Invoked' column in the execution_table
If no exception is thrown, what happens to the handlerInvoked variable?
AIt remains false
BIt becomes null
CIt becomes true
DIt throws an error
💡 Hint
Check variable_tracker for handlerInvoked values after step 1
Concept Snapshot
@ControllerAdvice catches exceptions from all controllers globally.
Use @ExceptionHandler methods inside it to define custom error responses.
When an exception occurs, normal controller response is skipped.
The global handler sends a custom response instead.
This helps centralize error handling in one place.
Full Transcript
When a user sends a request, it goes to a controller method. If the method runs fine, it returns a normal response. But if an exception happens, the normal response stops. Instead, the exception is caught by a global handler marked with @ControllerAdvice. This handler runs its method to create a custom error response, like an error page. This way, all controllers share the same error handling logic. Variables like exceptionThrown and handlerInvoked track if an error happened and if the handler ran. This makes error handling simple and consistent across the app.