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

Errors are part of programming. No matter how carefully you write code, unexpected situations can occur. JavaScript provides powerful error-handling features that help developers to manage problems without crashing applications.

In this blog, we will learn what errors are, how to use try, catch, and finally blocks, how to create custom errors, and why proper error handling is important.

1. What Are Errors in JavaScript?

An error is a problem that occurs while a program is running. Errors can stop the execution of the code if they are not handled properly.

Runtime Error Example

Consider this code:

console.log(userName);

Output:

ReferenceError: userName is not defined

The variable userName does not exist, so JavaScript throws an error.

Another example:

let user = null;
console.log(user.name);

Output:

TypeError: Cannot read properties of null

These are examples of runtime errors that happen while the program is executing.

2. Using Try and Catch blocks

JavaScript provides the try...catch statement to handle errors safely.

Syntax

try {
    // Code that may cause an error
} catch (error) {
    // Handle the error
}

Example

try {
    console.log(userName);
} catch (error) {
    console.log("An error occurred.");
}

Output:

An error occurred.

Instead of crashing the program, the error is caught and handled.

3. The Finally Block

Sometimes you need code to run whether an error occurs or not.

This is where the finally block is useful.

Syntax

try {
    // Code
} catch (error) {
    // Handle error
} finally {
    // Always executes
}

Example

try {
    console.log(userName);
} catch (error) {
    console.log("Error handled");
} finally {
    console.log("Execution finished");
}

Output:

Error handled
Execution finished

The finally block runs regardless of whether an error occurs.

Common Uses of Finally

  • Closing database connections

  • Releasing resources

  • Cleaning up temporary data

  • Stopping loaders or spinners

4. Throwing Custom Errors

JavaScript allows developers to create and throw their own errors using the throw keyword.

Example

try {
  let age = 15;
  if (age < 18) {
     throw new Error("You must be at least 18 years old.");
   }
  console.log("Access granted");
} 
catch (error) {
  console.log(error.message);
}

Output:

Error: You must be at least 18 years old.

5. Why Error Handling Matters

Without error handling:

  • Applications may crash unexpectedly.

  • Users may see confusing error messages.

  • Important operations may stop working.

  • Debugging becomes difficult.

With proper error handling:

  • Applications fail gracefully.

  • Errors can be logged and tracked.

  • Users receive better feedback.

  • Developers can identify issues more easily.

This process is often called graceful failure, where the application continues working even when something goes wrong.

Try → Catch → Finally Execution Order

try {
    throw new Error("Something went wrong");
} catch (error) {
    console.log("Caught Error");
} finally {
    console.log("Finally Executed");
}

Output:

Caught Error
Finally Executed

Conclusion

Error handling is an essential part of JavaScript development. By using try, catch, and finally, developers can prevent applications from crashing and provide a better user experience. Custom errors allows to create meaningful messages for specific situations, while proper error handling makes debugging easier and helps applications fail gracefully when unexpected problems occur.