MODULE 10 - Exception Handling and Logging
Welcome to Module 10! πβ
Your application encounters an error... what does the user see? An unreadable Java stack trace? This module teaches you how to handle errors properly and trace what happens in your application.
π Learning Objectivesβ
By the end of this module, you will be able to:
- β Create custom exceptions
- β Use @ControllerAdvice for global handling
- β Return structured error responses
- β Log effectively with SLF4J
- β Configure Logback for different environments
π Prerequisitesβ
Before starting this module, you should have:
- β Completed Modules 1 to 9
- β Created REST controllers
- β Understood HTTP codes (4xx, 5xx)
- β Be familiar with Java exceptions
πΊοΈ Module Outlineβ
Theoretical Part (4 chapters)β
Chapter 1: Exception Handlingβ
Duration: 45 minutes
Content:
- Custom exceptions
- Exception hierarchy
- Best practices
- Checked vs Unchecked
Why important? The foundation of error handling
Chapter 2: @ControllerAdviceβ
Duration: 1 hour
Content:
- @ControllerAdvice and @RestControllerAdvice
- @ExceptionHandler
- Standardized error responses
- ProblemDetail (RFC 7807)
- ResponseStatusException
Why important? Centralized error handling
Chapter 3: Logging with SLF4Jβ
Duration: 45 minutes
Content:
- SLF4J and logging facades
- Log levels (TRACE β ERROR)
- Logger patterns
- MDC (Mapped Diagnostic Context)
- Lombok @Slf4j
Why important? Diagnose issues
Chapter 4: Logback Configurationβ
Duration: 45 minutes
Content:
- logback-spring.xml
- Appenders (Console, File, Rolling)
- Patterns and formatting
- Spring profiles
- JSON logs for production
Why important? Professional configuration
Practical Part (2 Labs)β
Lab 10.1: Centralized Error Managementβ
Duration: 1h30
Objective: Implement a complete error handling system
You will:
- Create business exceptions
- Implement a GlobalExceptionHandler
- Standardize error responses
- Test with different scenarios
Lab 10.2: Logging Systemβ
Duration: 1h30
Objective: Configure professional logging
You will:
- Configure Logback
- Create environment-specific logs
- Implement MDC for traceability
- File logs with rotation
π Total Durationβ
| Type | Duration |
|---|---|
| Theory | 3h15 (4 chapters) |
| Practice | 3h (2 Labs) |
| Total | 6h15 (~2 sessions) |
π― What You Will Understandβ
Before this Moduleβ
// Unhandled error π°
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userRepository.findById(id).orElseThrow();
// β 500 Internal Server Error with full stack trace!
}
// No logs
public void processOrder(Order order) {
// Does it work? Does it fail? No idea...
}
After this Moduleβ
// Clean error handling β
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
// β 404 {"type": "user-not-found", "title": "User not found", ...}
}
// Structured logging
@Slf4j
public class OrderService {
public void processOrder(Order order) {
log.info("Processing order {} for user {}", order.getId(), order.getUserId());
// ...
log.debug("Order total calculated: {}", total);
}
}
Clean error response:
{
"type": "https://api.example.com/errors/user-not-found",
"title": "User Not Found",
"status": 404,
"detail": "User with ID 123 was not found",
"instance": "/api/users/123",
"timestamp": "2024-01-15T10:30:00Z"
}
π Why This Module is Essentialβ
1. User Experienceβ
Without management:
- Stack trace exposed to client π±
- Cryptic error messages
- Hard to debug on the frontend
With management:
- Clear and localized messages
- Documented error codes
- Standardized JSON format
2. Debugging in Productionβ
Without logs:
- "The app crashed last night"
- "No idea what happened"
- Impossible to reproduce
With logs:
- Complete event trace
- User context (MDC)
- Filtering by level
3. Compliance with Standardsβ
RFC 7807 - Problem Details for HTTP APIs
βββ Standardized format
βββ Interoperability
βββ Automatic documentation
πΊοΈ Training Progressionβ
Module 10 = Production-ready application!
π How to Approach This Moduleβ
Recommended Progressionβ
- Chapter 1 β Understand Java exceptions
- Chapter 2 + Lab 10.1 β Implement global management
- Chapter 3 β Learn logging
- Chapter 4 + Lab 10.2 β Configure Logback
β Self-Assessmentβ
Before starting, answer:
- Do you know how to create a custom exception? β Yes β No
- Are you familiar with @ControllerAdvice? β Yes β No
- Do you use System.out.println for debugging? β Yes β No
- Do you know how to configure Logback? β Yes β No
If you answered "No" anywhere, this module is for you!
π― Next Stepβ
Ready to professionalize your error handling?
β‘οΈ Start with Chapter 1: Exception Handling