Chapter 18: Errors and exception handling
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Errors and exception handling
An exception interrupts normal execution. try runs code that may throw, catch handles an exception, and finally runs cleanup whether or not an exception occurred. Throw an Error with a useful message when appropriate.
How it works
Step-by-step walkthrough
- Create invalid JSON
- Catch the parsing error
- Run cleanup
Example
try {
JSON.parse('not JSON');
} catch (error) {
console.log('Invalid JSON');
} finally {
console.log('Done');
}Do not catch and silently ignore Playwright assertion failures.
✎ Your practice task
Replace the input with valid JSON.
Knowledge check
Which block is intended for unconditional cleanup?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.