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

  1. Get exception class with getClass().getSimpleName()
  2. getMessage() for error message
  3. Format timestamp appropriately

Related Exception Handling exercises

Practice all Exception Handling exercises · Run this idea in the Java compiler