Exception Logging in Java: Explanation & Practice
Log exception details properly
Problem summary
Catch exceptions and log them with timestamp, type, and message.
Starter code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Simulate exception
// Log with timestamp, type, message
}
}Expected output and test cases
- Logged exception
[2024-01-15 10:30:00] ArithmeticException: / by zero
- NPE logged
[2024-01-15 10:30:00] NullPointerException: null
- No exception
No errors
Hints
- Get exception class with getClass().getSimpleName()
- getMessage() for error message
- Format timestamp appropriately
Related Exception Handling exercises
- Practice Null Handling in Java
- Practice Resource Cleanup Pattern in Java
- Practice Retry Mechanism in Java
Practice all Exception Handling exercises · Run this idea in the Java compiler