6. Error Handling & Custom Exceptions

Try, Catch, Finally & Custom Errors

export class AutomationTimeoutError extends Error {
  constructor(message) {
    super(message);
    this.name = "AutomationTimeoutError";
  }
}

async function safeClick(action) {
try {
await action();
} catch (error) {
console.warn("Non-fatal element click failed, continuing:", error.message);
} finally {
console.log("Cleanup hook executed");
}
}

Task

  • Implement safeExecuteWithFallback:
  • 1. Accept primaryAction and fallbackAction async functions.
  • 2. Try executing primaryAction(). If it throws, execute fallbackAction() and return its result.
πŸ’‘Common doubts
Did you like the problem?
Lesson 6 of 35
JavaScript
Loading code editor…
Next