# Understanding Error Handling in JavaScript 

When writing JavaScript code, errors are normal.

They happen when something goes wrong while the program is running.  
Instead of letting the program crash, JavaScript allows us to **handle errors gracefully**.

This is called **error handling**.

* * *

### What Are Errors in JavaScript?

An error is something that stops your code from running properly.

Example:

```javascript
let num = 10

console.log(num.toUpperCase())
```

This will cause an error because `toUpperCase()` works on strings, not numbers.

Without handling, the program breaks.

* * *

## Using try and catch

JavaScript gives us `try...catch` to handle errors.

```javascript
try {
  let num = 10
  console.log(num.toUpperCase())
} catch (error) {
  console.log("Something went wrong")
}
```

How it works:

*   `try` → runs the code
    
*   if error happens → `catch` runs
    

So instead of crashing, we handle the problem.

* * *

![https://images.openai.com/static-rsc-4/y3QkIZsUHb4IUtFKf-e-YgvF8mHEmKSG3OUj9-PdSb8aQErwsErwzRASfN5stc6rwR90i_TNSiEEyMfKRExymK1B7FdnCPIK5oImtJflBUKIZ3sQ-JhOFAwd-Mls49_5U8r-wus0x1SZYkPaGRz7w2yNs-y7PVKFbp3Mvya3SxsPmLXi7Icn2BFDVylj7TpF?purpose=fullsize](https://images.openai.com/static-rsc-4/34-iUn9ececJVg6sYQ3Pdil4UR8G2G7L767APur7jX1K8dq6LK_vba_4FZ_x5wnwOwC6OhE2ttFoYleqAmykhCLyS-mnTxBkkLsXTNE-wCG0SfZELfpK3IkbwDgzG8bz--EXKZ6Tla3M5RUWIB0EykSswjirzr5OovpPqVJHzys?purpose=inline align="center")

* * *

### The finally Block

The `finally` block runs no matter what.

```javascript
try {
  console.log("Trying code")
} catch (error) {
  console.log("Error occurred")
} finally {
  console.log("Always runs")
}
```

Even if there is no error, `finally` will still execute.

* * *

![https://images.openai.com/static-rsc-4/qLB_8Lc7WKtiqE12eFKPSErNnx5WWOReaimZfcOU4n9YVmEdA0jUY7lfKBDmlfBf2BuwIw1KcV5WJ0D3OCXmpA6mdzAMhGAqdRc6hWdu0x5wH8MF-Gr2UXbQraI9T5IqDsgwUOd4-wAtlhDACVMlQCPHlUQAjLcFtxfNPk2ICgVGJL_2A0JG_10bbYoO-EYw?purpose=fullsize](https://images.openai.com/static-rsc-4/wlIbcSppRu6xfNWu922y2nfzyznJx9T30bYttfopPJKU9XEqumYwQU3-MkRuTsrBO2KchaQ8a3Orm7-b0QGxWHzRH6fh5QBGERLeACH1zapvKJ-2OFXvH5kvwNyB-GmrdRrHUe8fJMmhA8gqxV7hYMaP7xtk8J9D51WR3WHHaVM?purpose=inline align="center")

* * *

## Throwing Custom Errors

You can also create your own errors using `throw`.

```javascript
let age = 15

try {
  if (age < 18) {
    throw "You must be 18 or older"
  }
  console.log("Access granted")
} catch (error) {
  console.log(error)
}
```

This allows you to control when errors should happen.

* * *

## Why Error Handling Matters

Without error handling:

*   your program crashes
    
*   users see broken output
    
*   debugging becomes harder
    

With error handling:

*   you control failures
    
*   you show meaningful messages
    
*   your program continues running
    

This is called **graceful failure**.

* * *

### How to Think About It

Instead of letting errors break your program:

*   try the code
    
*   catch problems
    
*   handle them properly
    

* * *

### One Line Summary

Error handling helps your program fail safely instead of crashing.

* * *

### Conclusion

Errors are a normal part of programming, but handling them properly makes your code more reliable.

Using `try`, `catch`, `finally`, and `throw`, you can control how your program reacts when something goes wrong.

This not only improves user experience but also makes debugging easier.
