# Throwing, catching and handling errors

*JavaScript Developer Study Guide · Debugging and Error Handling · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/javascript-developer/debugging-and-error-handling/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.

- Certification: Salesforce JavaScript Developer
- Exam section: Debugging and Error Handling
- Study guide: https://www.certifyforce.com/study-guide/javascript-developer

**Official exam objective:**

> Given a scenario, handle errors properly.

## 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

| Fact | Detail | Source |
|---|---|---|
| After `throw` | Remaining statements skipped; nearest `catch` up the call stack runs | [throw](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw) |
| `finally` | Always runs before control leaves the statement, caught or not | [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) |
| `return` in `finally` | Overrides a pending `return` or `throw` from `try`/`catch` | [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) |
| What can be thrown | Any expression; `catch` gets exactly that value | [Control flow and error handling](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling) |
| `Error` properties | `name`, `message`; `stack` is de facto in all major engines | [Error.prototype.stack](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack) |
| Invalid JSON string | `JSON.parse` throws `SyntaxError` | [JSON.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) |
| Undeclared variable | `ReferenceError` | [ReferenceError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) |
| Window `error` event | Script errors thrown synchronously (not promise rejections) | [error event](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event) |
| `unhandledrejection` | Promise rejected with no handler (not fired for rejections from a cross-origin script); `event.reason` holds the value | [unhandledrejection event](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event) |

## Check yourself

- 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?

## Sources

- [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch)
- [Control flow and error handling](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling)
- [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
- [throw](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw)
- [Error.prototype.stack](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack)
- [TypeError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError)
- [RangeError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError)
- [SyntaxError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
- [ReferenceError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError)
- [JSON.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
- [Window: error event](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event)
- [Window: unhandledrejection event](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event)
- [Window: rejectionhandled event](https://developer.mozilla.org/en-US/docs/Web/API/Window/rejectionhandled_event)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 11 linked practice questions, and unlimited timed mock exams are included with the JavaScript Developer pack (USD $19): https://www.certifyforce.com/practice-exams
