Skip to content
Guest
Learn JavaScript · Course Syllabus Show lessons
Async & Modern JS · 8 min read · Beginner

Error Handling & Debugging

Write resilient code by validating inputs, throwing descriptive Error objects, catching failures cleanly, and using console debugging tools.

What you'll learn

  • Throw meaningful Error instances when invalid arguments or states occur
  • Guard risky parsing or network code with try / catch
  • Use console.log, console.warn, console.error, and console.table effectively

Bugs happen in every codebase. The difference between brittle code and production-grade code is failing loudly with clear error messages during development and recovering gracefully for end users.

Throwing Descriptive Errors

function divide(a, b) {
  if (b === 0) {
    throw new Error('Cannot divide by zero');
  }
  return a / b;
}

Fill-in-the-Blank Exercise

Throw & Catch Errors Safely

Throw a new Error when input is empty, and catch it cleanly.

function parseName(str) {
if (!str.trim())  new Error('Name is required');
return str.trim();
}
try {
parseName('');
}  (err) {
console.error(err.message);
}

Try It Yourself

Test valid JSON vs. malformed JSON below and watch how try / catch prevents the app from crashing:

Safe JSON Validator with try/catch
Live Result
Console Output

No console output yet. Try calling console.log(...) in JS.

Check Your Understanding

Score: 0 / 1

1. Why is `throw new Error('Invalid ID')` better than `throw 'Invalid ID'`?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind