# Error Handling in JavaScript: Try, Catch, Finally

If you have write code any time , this topic seems familiar to you. While writing JavaScript applications, errors are completely normal. Even experienced developers face errors daily while working on APIs, databases, user inputs, UI and etc. The most important thing not avoiding all errors.

The important thing is Handling errors properly. JavaScript provides powerful error handling features like try, catch , finally and throw that help applications behave safely even when something goes wrong.

* * *

# What errors are in JavaScript

Errors are code that create unexpected problems that stops code execution. Let's take an example , `console.log(username)` , can you guess the output, output : `ReferenceError: userName is not defined` because `username` is not defined here or does not exist. JavaScript throws an error.

### Why Errors Happen

Errors can happen because of many reasons like wrong variable names, invalid syntax, API failures, user mistakes, network problems, incorrect calculations and many more can cause errors. Errors are part of real -world programming.

### What happens without Error handling

Without proper Error handling the program gets crashed immediately and never executes. This can break applications due to single variables or a undefined it can break an applications badly. That is why error handling matters.

### Why Error Handling Matters

Good developer always handle error that helps them to :

*   prevent applications crashes
    
*   improve user experience
    
*   debug problems easily
    
*   make applications reliable
    
*   handle unexpected situations safely
    

* * *

# Using try and catch blocks

JavaScript provides error handling techniques know as try-catch blocks for handling errors safely. Basic syntax are :

```javascript
try {
  // risky code , that can crash the program
} catch (error) {
  // handle error
}
```

Let's implement this through an example :

```javascript
try {
  console.log(userName);
} catch (error) {
  console.log("Something went wrong");
}
```

output :

```javascript
Something went wrong
```

Here, one thing to notice is that the program does not stop execution , instead of crashing , the error gets handled with well manner.

## Understanding the flow

![](https://cdn.hashnode.com/uploads/covers/63c43a8adbe7ab4342a06690/176b297a-a888-46fd-8a13-fe473ec11981.png align="center")

* * *

# The finally block

JavaScript also provides finally with try-catch block. The finally always runs whether error happens or no error took place , but this code always run and gets execute. The Syntax is similar to try-catch :

```javascript
try {

} catch(error) {

} finally {

}
```

Take an example :

```javascript
try {
  console.log("Inside try");
} catch(error) {
  console.log("Inside catch");
} finally {
  console.log("Always runs");
}

// OUTPUT :
// Inside try
// Always runs
```

### Why `finally` is Useful

Finally is commonly used for :

*   cleanup tasks
    
*   closing database connections
    
*   stopping loaders
    
*   releasing resources
    

Because it always execute no matter what gets above this `finally` block always gets executed.

* * *

# Throwing custom errors

JavaScript also allows developers to create their own errors using `throw` . Sometimes JavaScript itself does not know something is wrong. This is classic example if you written code , then you have notice this. JavaScript will not automatically throws error here , we need to manually create it. Basic example of `throw` :

```javascript
const age = -5;

if (age < 0) {
  throw new Error("Age cannot be negative");
}
```

Output:

```javascript
Error: Age cannot be negative
```

Here, we manually told JavaScript to throw error with custom message inside curly brackets.

* * *

# Why error handling matters

![](https://cdn.hashnode.com/uploads/covers/63c43a8adbe7ab4342a06690/add77f99-2f00-45e9-b2b9-ab24f78fd077.png align="center")

* * *

# Wrapping Up 📍

Thanks for reading till the end 🙌  
I hope this article helped you understand the topic in a simple, practical, and beginner-friendly way.

My goal is to break down complex tech concepts into clear, real-world explanations, especially for learners who are just starting out or feeling overwhelmed.

If you found this useful, feel free to bookmark, share, or leave a comment - it really helps and keeps me motivated to write more.

You can connect with me here:

*   🐦 [**X (Twitter)**](https://x.com/RahulDe13551305)
    
*   💼 [**LinkedIn**](https://www.linkedin.com/in/devrahulll)
    

Let’s learn together and grow step by step 🚀

* * *
