Skip to main content
Module 5 · Lesson 4 of 420/32 in the course~18 min
Module lessons (4/4)

try / catch / throw

Lesson preview5 hands-on exercises readyJavaScript Course · Module 5 — Control flow

Raise and catch errors, separate normal flow from exceptional flow.

What you'll learn in this lesson

  • Raising explicit errors with throw new Error('messaggio') at the edge of critical logic.
  • Catch synchronous exceptions safely with try / catch / finally.
  • Distinguish when to use try / catch (JSON parsing, I/O, untrusted external inputs) versus prior validations with if.
  • Transform operations that launch into result objects typed { ok: true, value } | { ok: false, error }.

Overview

An error in JavaScript immediately stops execution of the current function and moves up the Call Stack until it encounters a try / catch block that handles it — otherwise it terminates the program as an unhandled exception.

Sections covered in this lesson

  1. 1Mental model: throwing (throw) and catching (try/catch/finally)
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 4Check for understanding
Pro Lesson · Module 5 — Control flow

Unlock "try / catch / throw" and the full JavaScript Course curriculum

Raise and catch errors, separate normal flow from exceptional flow.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in JavaScript Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.