Technical Mid Level

How do you structure error handling in a JavaScript application? How do you handle errors in async code and at application boundaries?

Quick Tip

Show the distinction: "Programmer errors like TypeError should crash and be fixed. Operational errors like network timeouts should be caught, retried, and surfaced to the user gracefully. I handle them differently."

What good answers include

Patterns: try/catch for synchronous and async/await code, .catch() for Promise chains, global handlers (window.onerror, process.on uncaughtException/unhandledRejection), error boundary components in React. Best practices: create custom Error subclasses for different error types, always log with context, fail fast for programming errors, recover gracefully from operational errors. Strong candidates distinguish between programmer errors (bugs, should crash) and operational errors (network failures, should retry or degrade gracefully).

What interviewers are looking for

Tests code robustness thinking. Candidates who use empty catch blocks or who do not handle Promise rejections will write fragile applications. Look for a systematic approach with custom error types and clear recovery strategies.

← All JavaScript questions