try / catch / finally
Error types
| Type | When thrown |
|---|---|
Error | Base class for all errors |
TypeError | Wrong type used |
ReferenceError | Undefined variable accessed |
SyntaxError | Invalid JS syntax |
RangeError | Value out of valid range |
URIError | Invalid URI |
Custom errors
Async error handling
Error propagation patterns
Common interview questions
What happens if you throw inside a finally block?
What happens if you throw inside a finally block?
The error from
finally replaces the original error (or return value). The original error is lost. Avoid throwing in finally — use it only for cleanup.How do you handle errors in Promise.all?
How do you handle errors in Promise.all?
Promise.all rejects as soon as any promise rejects. The other promises continue running but their results are ignored. Use Promise.allSettled if you need all results regardless of failures, or wrap individual promises in .catch() to prevent Promise.all from short-circuiting.Why should you extend Error for custom errors?
Why should you extend Error for custom errors?
Extending
Error gives you: a stack trace, proper instanceof checks, a message property, and correct behavior in error monitoring tools. Always set this.name in the subclass so the error displays correctly.