8. Robust Exception Handling & Custom Failures

Java Exception Hierarchy in Automation

- Throwable: Root class. - Error: Fatal JVM issues (OutOfMemoryError). - Exception: Application issues we can catch. - RuntimeException (Unchecked): NullPointerException, IllegalArgumentException, TimeoutException. - Checked Exceptions: IOException, InterruptedException (must be declared or handled).
public class AutomationErrorHandler {
    public static void executeWithRetry(Runnable action) {
        try {
            action.run();
        } catch (RuntimeException e) {
            System.err.println("Action failed: " + e.getMessage());
            throw new AutomationException("Automation step failed: " + e.getMessage(), e);
        } finally {
            System.out.println("Step execution completed cleanup.");
        }
    }
}

Task

  • Implement safeParseInt:
  • 1. Accept String value and int defaultValue.
  • 2. Inside try block, parse using Integer.parseInt(value).
  • 3. If NumberFormatException occurs, catch it and return defaultValue.
πŸ’‘Common doubts
Did you like the problem?
Lesson 8 of 38
Java
Loading code editor…
Next