JavaScript · Error Handling · Senior · Scenario
What global error hooks exist in browsers/Node, and what should they do vs not do?
Short Interview Answer
Browsers: `window.onerror`, `unhandledrejection`. Node: `uncaughtException`, `unhandledRejection`. Use for logging/telemetry; don't use as normal control flow.
Detailed Explanation
Global handlers are safety nets for unexpected failures — report to Sentry/etc., show a generic recovery UI, and in Node often exit on uncaughtException because state may be corrupt. Prefer local handling for expected paths. Ensure handlers themselves don't throw. Filter noisy aborted fetches. Include release/version metadata. For SPAs, pair with route-level boundaries. Document that covering every error globally does not replace thoughtful API design.
Example
window.addEventListener('unhandledrejection', (e) => {
report(e.reason);
});Interview Tip
Say 'last resort, not business logic'.
Common Mistake
Swallowing unhandledrejection without logging, hiding production bugs.