Debugging and Error Handling7% of the exam
Throwing, catching and handling errors
How throw, try, catch and finally move control (including return in finally and rethrowing from nested handlers), why to throw Error objects, how to tell TypeError, RangeError, SyntaxError and ReferenceError apart with instanceof, where to place a handler for loops and async code, and when the global error and unhandledrejection events fire.
Given a scenario, handle errors properly.
~6 min read · 11 practice questions ·
What the exam expects
- Trace
throw,try,catchandfinally, including nested handlers, rethrowing, andreturninsidefinally. - Throw
Errorobjects (or subclasses) rather than strings or plain objects, and readname,messageandstack. - Identify which built-in error a failure raises (
TypeError,RangeError,SyntaxError,ReferenceError) and branch on it withinstanceofin the right order. - Place the handler where it can actually catch: per item inside a loop, around an
await, or with.catch(); useerrorandunhandledrejectionas last-resort global hooks. - Key terms: exception (a thrown value that unwinds the call stack); catch binding (the
errincatch (err), holding exactly what was thrown); finally (cleanup that runs however thetryexits); rethrow (throwing again from acatchso an outer handler sees it); rejection (the async form of a thrown error).
Key facts
After
throw- Detail
- Remaining statements skipped; nearest
catchup the call stack runs
finally- Detail
- Always runs before control leaves the statement, caught or not
return in finally- Detail
- Overrides a pending
returnorthrowfromtry/catch
What can be thrown
- Detail
- Any expression;
catchgets exactly that value
Error properties- Detail
name,message;stackis de facto in all major engines
Invalid JSON string
- Detail
JSON.parsethrowsSyntaxError
Undeclared variable
- Detail
ReferenceError
Window
error event- Detail
- Script errors thrown synchronously (not promise rejections)
unhandledrejection- Detail
- Promise rejected with no handler (not fired for rejections from a cross-origin script);
event.reasonholds the value
Sources for this section (8)
- throwdeveloper.mozilla.org (opens in a new tab)
- try...catchdeveloper.mozilla.org (opens in a new tab)
- Control flow and error handlingdeveloper.mozilla.org (opens in a new tab)
- Error.prototype.stackdeveloper.mozilla.org (opens in a new tab)
- JSON.parse()developer.mozilla.org (opens in a new tab)
- ReferenceErrordeveloper.mozilla.org (opens in a new tab)
- error eventdeveloper.mozilla.org (opens in a new tab)
- unhandledrejection eventdeveloper.mozilla.org (opens in a new tab)
- Why should code throw `new Error('…')` rather than a string?
- When does a `return` in `finally` change a function's result?
- Why does `err instanceof Error` match a `TypeError`?
- Which window event reports an error thrown inside an async function that nobody awaited?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (11 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams