Use try/catch/finally for sync errors and await failures. Throw Error objects with messages.
Custom errors
Subclass Error for domain failures—stack traces aid debugging.
User-facing
Log full error server-side; show generic message client-side—no stack traces to users.
Important interview questions and answers
- Q: throw string?
A: Avoid—throw Error for stack traces. - Q: finally?
A: Runs always—cleanup resources.
Self-check
- Why try/catch with await?
- What belongs in finally?
Tip: Re-run the playground code for error-handling-try-catch and tweak one line before the MCQs.
Interview prep
- throw Error?
Preserves stack trace for debugging.