Spring Boot Exception Handling Interview Questions and Answers
Master Spring Boot Exception Handling with interview questions covering @ExceptionHandler, @ControllerAdvice, @RestControllerAdvice, Problem Details (RFC 9457), custom exceptions, ResponseStatusException, validation errors, and production best practices.
Introduction
Exception handling is one of the most important topics in Spring Boot interviews.
A production application should never expose stack traces or internal implementation details to API consumers.
Instead, it should return:
- Proper HTTP Status Codes
- Consistent Error Responses
- Meaningful Error Messages
- Trace IDs
- Validation Details
- Timestamps
Spring Boot provides several mechanisms for handling exceptions globally and consistently.
Spring Boot Exception Handling Architecture
flowchart LR
Client --> DispatcherServlet
DispatcherServlet --> Controller
Controller --> Service
Service --> Exception
Exception --> GlobalExceptionHandler
GlobalExceptionHandler --> JSONResponse
JSONResponse --> Client
Q1. What is Exception Handling in Spring Boot?
Answer
Exception Handling is the process of catching runtime errors and returning meaningful responses instead of application crashes.
Example
Customer Not Found
↓
404 NOT FOUND
↓
JSON Error Response
Benefits
- Better user experience
- Secure APIs
- Standardized responses
- Easier debugging
Q2. What is @ExceptionHandler?
@ExceptionHandler handles specific exceptions.
Example
@ExceptionHandler(
CustomerNotFoundException.class)
public ResponseEntity<String>
handleException(){
return ResponseEntity
.status(404)
.body("Customer Not Found");
}
Scope
- Controller-specific
- Handles selected exception types
Q3. What is @ControllerAdvice?
@ControllerAdvice provides global exception handling across all controllers.
Example
@ControllerAdvice
public class GlobalExceptionHandler {
}
Architecture
flowchart TD
ControllerA --> ControllerAdvice
ControllerB --> ControllerAdvice
ControllerC --> ControllerAdvice
ControllerAdvice --> ErrorResponse
Advantages
- Centralized logic
- Reusable
- Consistent API responses
Q4. What is @RestControllerAdvice?
@RestControllerAdvice combines
@ControllerAdvice@ResponseBody
Example
@RestControllerAdvice
public class GlobalHandler {
}
Recommended for REST APIs because responses are automatically serialized to JSON.
Q5. What is ResponseStatusException?
ResponseStatusException allows returning an HTTP status without creating a custom exception.
Example
throw new
ResponseStatusException(
HttpStatus.NOT_FOUND,
"Customer not found");
Useful for
- Simple APIs
- Rapid development
- Small projects
For enterprise systems, custom exceptions are generally preferred.
Q6. What is ProblemDetail?
Spring Framework 6 / Spring Boot 3 introduced ProblemDetail, which follows RFC 9457 for standardized API error responses.
Example
ProblemDetail problem =
ProblemDetail.forStatus(
HttpStatus.NOT_FOUND);
problem.setTitle(
"Customer Not Found");
Example Response
{
"type":"about:blank",
"title":"Customer Not Found",
"status":404,
"detail":"Customer 101 does not exist"
}
This is the recommended approach for new Spring Boot 3 applications.
Q7. How do you handle Validation Exceptions?
Validation failures occur when Bean Validation fails.
Example
@PostMapping
public Customer save(
@Valid
@RequestBody
CustomerRequest request){
}
Global Handler
@ExceptionHandler(
MethodArgumentNotValidException.class)
Validation Flow
flowchart LR
ClientRequest --> Validation
Validation --> Success
Validation --> Exception
Exception --> GlobalHandler
GlobalHandler --> JSONError
Q8. How do you create Custom Exceptions?
Example
public class
CustomerNotFoundException
extends RuntimeException{
public CustomerNotFoundException(
String message){
super(message);
}
}
Usage
throw new
CustomerNotFoundException(
"Customer 101 not found");
Benefits
- Business-specific errors
- Cleaner code
- Better readability
Q9. What should a Production Error Response contain?
Recommended fields
- Timestamp
- Status
- Error Code
- Message
- Path
- Trace ID
- Correlation ID
Example
{
"timestamp":"2026-07-12T12:30:00Z",
"status":404,
"error":"NOT_FOUND",
"message":"Customer not found",
"path":"/customers/101",
"traceId":"9af3b7c2"
}
Never expose stack traces in production responses.
Q10. Exception Handling Best Practices
Use Global Exception Handling
Prefer @RestControllerAdvice.
Return Proper HTTP Status Codes
Examples
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 409 Conflict
- 500 Internal Server Error
Use Custom Exceptions
Represent business failures clearly.
Log Exceptions
Log complete details internally while returning safe responses to clients.
Include Trace IDs
Enable end-to-end request tracking.
Banking Example
flowchart TD
TransferRequest --> Controller
Controller --> TransferService
TransferService --> Exception
Exception --> GlobalExceptionHandler
GlobalExceptionHandler --> ProblemDetail
ProblemDetail --> Client
The client receives a standardized error response while the full stack trace is retained in server logs.
Common Interview Questions
- What is
@ExceptionHandler? - What is
@ControllerAdvice? @ControllerAdvicevs@RestControllerAdvice?- What is
ResponseStatusException? - What is
ProblemDetail? - How do you handle validation exceptions?
- How do you create custom exceptions?
- What should an API error response contain?
- How do you secure exception handling?
- Exception handling best practices?
Quick Revision
| Topic | Summary |
|---|---|
| @ExceptionHandler | Handles specific exceptions |
| @ControllerAdvice | Global exception handling |
| @RestControllerAdvice | Global REST exception handling |
| ResponseStatusException | Return HTTP status directly |
| ProblemDetail | Standard RFC 9457 error response |
| Custom Exception | Business-specific exception |
| Validation Exception | Bean Validation failure |
| Trace ID | Request correlation |
| HTTP Status | Correct REST response codes |
| Logging | Log internally, hide details externally |
Exception Handling Lifecycle
sequenceDiagram
Client->>Controller: HTTP Request
Controller->>Service: Business Logic
Service-->>Controller: Exception
Controller->>GlobalExceptionHandler: Handle Exception
GlobalExceptionHandler->>ProblemDetail: Create Error Response
ProblemDetail-->>Client: JSON Response
Production Example – Banking Fund Transfer API
A customer requests a fund transfer.
Scenario
-
Source account does not exist.
-
The service throws
AccountNotFoundException. -
@RestControllerAdvicecatches the exception. -
A
ProblemDetailresponse is created. -
The response includes:
- HTTP 404
- Error title
- Detail message
- Request path
- Trace ID
-
The complete stack trace is logged to ELK for troubleshooting.
flowchart LR
MobileApp --> TransferAPI
TransferAPI --> TransferService
TransferService --> AccountNotFoundException
AccountNotFoundException --> GlobalExceptionHandler
GlobalExceptionHandler --> ProblemDetail
ProblemDetail --> MobileApp
GlobalExceptionHandler --> ELK
This approach provides secure, standardized, and traceable error handling suitable for enterprise systems.
Key Takeaways
- Spring Boot provides multiple mechanisms for handling exceptions, including
@ExceptionHandler,@ControllerAdvice,@RestControllerAdvice, andResponseStatusException. @RestControllerAdviceis the preferred solution for centralized exception handling in REST APIs.- ProblemDetail (RFC 9457) is the recommended error response model for Spring Boot 3 and Spring Framework 6 applications.
- Create custom exceptions to represent business-specific failures instead of using generic runtime exceptions.
- Handle validation failures globally by catching
MethodArgumentNotValidException. - Return appropriate HTTP status codes and avoid exposing stack traces or sensitive implementation details.
- Include metadata such as Trace ID, timestamp, and request path to improve observability and troubleshooting.
- A standardized exception handling strategy improves API consistency, security, maintainability, and production support.