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.

Official exam objective
Given a scenario, handle errors properly.

~6 min read · 11 practice questions ·

What the exam expects

  • Trace throw, try, catch and finally, including nested handlers, rethrowing, and return inside finally.
  • Throw Error objects (or subclasses) rather than strings or plain objects, and read name, message and stack.
  • Identify which built-in error a failure raises (TypeError, RangeError, SyntaxError, ReferenceError) and branch on it with instanceof in the right order.
  • Place the handler where it can actually catch: per item inside a loop, around an await, or with .catch(); use error and unhandledrejection as last-resort global hooks.
  • Key terms: exception (a thrown value that unwinds the call stack); catch binding (the err in catch (err), holding exactly what was thrown); finally (cleanup that runs however the try exits); rethrow (throwing again from a catch so an outer handler sees it); rejection (the async form of a thrown error).

Key facts

After throw
Detail
Remaining statements skipped; nearest catch up the call stack runs
finally
Detail
Always runs before control leaves the statement, caught or not
return in finally
Detail
Overrides a pending return or throw from try/catch
What can be thrown
Detail
Any expression; catch gets exactly that value
Error properties
Detail
name, message; stack is de facto in all major engines
Invalid JSON string
Detail
JSON.parse throws SyntaxError
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.reason holds the value
Sources for this section (8)
  • 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

Practice for the JavaScript Developer exam