Playwright + Java · Lesson 8 of 38

0/38 done

Module 8: Exception Handling

8. Robust Exception Handling & Custom Failures

15 minlesson

Your 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.

Write it in the editor, then press Check answer (Ctrl + Enter).

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).
java
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.");
        }
    }
}

Key Takeaways & Walkthrough Summary

Automated tests execute in unpredictable environments. Timeouts, missing network connectivity, and element failures will occur. In this lesson, you will master try-catch-finally, checked vs unchecked exceptions, and custom exceptions to prevent cryptic test crashes. Let's master error handling!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Should I catch generic 'Exception' in test methods?

Avoid broad catch blocks that suppress test failures. Only catch specific expected exceptions where recovery or friendly logging is intended.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip