0Pricing
JavaScript Academy · Lesson

try/catch/finally, throw, custom errors

Use try/catch/finally, throw simple errors, and define a tiny custom Error for clearer messages.

Intro to error handling

Goal: Catch errors safely and keep programs stable.

  • try/catch basics
  • throw with a clear message
  • finally for cleanup
  • tiny custom Error
try/catch/finally, throw, custom errors — illustration 1

try/catch basics

Wrap risky operations with try. In catch, show a short, helpful message.

// try/catch around risky code
try {
  const data = JSON.parse("{\"name\":\"Ayla\"}");
  console.log("Parsed:", data.name);
} catch (e) {
  // e.message is short and useful on mobile
  console.log("Failed to parse:", e.message);
}
try/catch/finally, throw, custom errors — illustration 2

All lessons in this course

  1. try/catch/finally, throw, custom errors
  2. Defensive Programming & Input Checks
← Back to JavaScript Academy