FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Errors & Debugging

Advanced

try, catch & throw

try, catch and throw let you protect risky code, handle expected failures and create your own errors.

function requireTitle(record) {
  if (!record.title) {
    throw new Error("Title is required");
  }
}

Errors & Debugging

try catches thrown errors; throw creates them.

Code inside try runs normally until something throws. When an error is thrown, JavaScript jumps to catch.

throw should be used when a function cannot honestly return a useful result. It communicates that normal execution must stop.

A catch block should either handle the problem, convert it into a useful result or rethrow it. Catching everything and doing nothing is worse than crashing loudly.

try

Wraps code that may throw.

catch

Receives the thrown error.

throw

Creates an error path.

finally

Runs after try/catch whether success or failure happened.

Examples

Good debugging code keeps the failure path visible.

Throw when the contract is broken

function createRecord(input) {
  if (typeof input.title !== "string") {
    throw new TypeError("title must be a string");
  }

  return { title: input.title.trim() };
}

Returning unclear failure values

function createRecord(input) {
  if (!input.title) {
    return false;
  }
}

// The caller has to guess what false means.

Code patterns

Reusable examples for quick reference.

These examples focus on the debugging patterns you will reuse: safe parsing, throwing, custom error types, console inspection, breakpoints and async failure handling.

Throw validation error

Stop when required input is missing.

function requireTitle(record) {
  if (!record.title) {
    throw new Error("Title is required");
  }
}

Catch parse error

Handle risky parsing.

try {
  const data = JSON.parse(raw);
} catch (error) {
  console.error(error.message);
}

Rethrow with context

Add context without hiding the cause.

try {
  parseConfig(raw);
} catch (error) {
  throw new Error("Could not parse config", { cause: error });
}

finally

Clean up regardless of outcome.

let busy = true;
try { runTask(); }
finally { busy = false; }

Rules that matter

Make failures observable and recoverable where possible.

Debugging starts before something breaks. Clear error types, helpful messages and visible failure states make the code easier to repair.

Throw real Error objects

throw new Error gives stack and message.

Catch at useful boundaries

Catch where you can recover or add context.

Do not catch too early

Low-level code may not know how to recover.

Use finally for cleanup

It runs after success and failure.

Rethrow when needed

Do not hide failures you cannot handle.

Keep catch blocks focused

A catch block should not become unrelated logic.

Production thinking

Reliable software is built by handling failure deliberately.

Why does this matter?

try/catch/throw gives JavaScript a clear failure path instead of returning confusing values from every function.

Accessibility

Caught errors should become understandable UI messages when users can take action.

Production note

Production boundaries should catch expected errors and report unexpected ones.

SEO note

Rendering code should fail visibly during development rather than silently output incomplete content.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Change the thrown message.
  • Throw a TypeError instead of Error.
  • Add a finally block that logs cleanup.

Practice assignment

Do this before moving to the next topic.

  1. Write one function that throws.
  2. Catch that error.
  3. Add one finally block.

Try it yourself

Throw and catch a validation error

Live preview

Self-check

Before you continue, prove that you understand try, catch & throw.

Advanced

A JavaScript feature is not production-ready until you know how it fails and how you will inspect that failure.

  1. Can you explain try?
  2. Can you explain catch?
  3. Can you explain throw?
  4. Can you explain finally?
  5. Can you explain when to rethrow?