Skip to main content

Command Palette

Search for a command to run...

Error Handling in JavaScript: Try, Catch, Finally

Updated
3 min read
Error Handling in JavaScript: Try, Catch, Finally

1.What are Errors in JavaScript?

Errors are problems that occur during program execution (runtime) and stop your code from working properly.

Example of a Runtime Error

console.log(user.name); // user is not defined

This throws a ReferenceError and crashes the program.

2. Why Error Handling Matters

In a perfect world, code always works. In reality, users enter letters into number fields, servers go down, and APIs return unexpected results.

  • Graceful Failure: Instead of the entire application "white-screening" or freezing, error handling allows the app to show a helpful message and keep running.

  • Debugging Benefits: Proper handling provides clear logs. Instead of seeing a vague Uncaught TypeError, you can log a specific message like Failed to load user profile: Database timeout.

Runtime Error Example

A runtime error occurs while the code is executing (unlike syntax errors, which prevent the code from even starting).

const user = null;
console.log(user.name); // ❌ Uncaught TypeError: Cannot read properties of null
console.log("This will never run"); // The script stops here

3. Using Try and Catch Blocks

The try...catch statement consists of a try block and a catch block.

  • Try: Contains the code that might cause an error.

  • Catch: Contains the code to execute if an error occurs.

Basic Syntax

try {
  // risky code
} catch (error) {
  // handle error
}

Example

try {
  let data = JSON.parse("invalid json");
} catch (error) {
  console.log("Something went wrong:", error.message);
// The script continues to run safely after this
}

4. The Finally Block

The finally block is the "cleanup" crew. It executes no matter what, whether an error was thrown or not. This is perfect for closing database connections or hiding loading spinners.

Execution Order:

  1. Try runs.

  2. If error: Catch runs.

  3. Finally runs (always).

Syntax

try {
  // code
} catch (error) {
  // handle error
} finally {
  // always runs
}

Example

try {
  console.log("Trying...");
} catch (err) {
  console.log("Error occurred");
} finally {
  console.log("Always executed");
}

5. Throwing Custom Errors

Sometimes, code is technically "correct" but logically wrong (e.g., a user entering a negative age). You can use the throw keyword to create your own errors.

function checkAge(age) {
  if (age < 0) {
    throw new Error("Age cannot be negative!"); // Custom error
  }
  return true;
}

try {
  checkAge(-5);
} catch (e) {
  console.log(e.name);    // "Error"
  console.log(e.message); // "Age cannot be negative!"
}

6. Types of Common Errors

Common errors you'll encounter in JavaScript include:

  • ReferenceError: Using a variable that hasn't been declared.

  • TypeError: Performing an operation on the wrong data type (like calling a string as a function).

  • SyntaxError: Writing code that the engine can't interpret (usually caught before runtime).

7.Real-World Use Cases

  • API calls (handling failed requests)

  • JSON parsing

  • Form validation

  • File operations

  • Payment systems

JavaScript form basic concepts to Advance

Part 8 of 17

Master JavaScript from the ground up. This series takes you on a complete journey from fundamental syntax and variables to advanced asynchronous patterns, closures, and performance optimization. Whether you are a beginner writing your first line of code or an intermediate developer looking to bridge the gap to seniority, these guides provide deep dives, practical examples, and modern best practices for the 2026 ecosystem.

Up next

Async/Await in JavaScript: Writing Cleaner Asynchronous Code

In the early days of JavaScript, we handled asynchronous operations with Callbacks, which led to "Callback Hell." Then came Promises, which were better but still felt a bit "clunky" with nested .then(