Error Handling in JavaScript.
Try, Catch, Finally.

What errors are in JavaScript.
So from the word error, you already get an idea about topic. As everyone knows, an error means something went wrong in the code output or you can also say an unexpected output, that is called error. It means you expected something from your input, but when the program run, it resulted in an error.
Now, in the programming world, for a developer, this is a very common thing. Whenever you write code and run it, in most cases, getting an error on the first attempt is expected. Every developer faces many errors.
These error can be syntax error, for example , if you use the syntax of programming language incorrectly, it will give an error. If you declare a wrong value in the wrong place, it will give an error. Using a wrong data type like a string in the wrong context can also cause errors. so these are basic errors.
But there are also some error that do not come at the time of declaration. They appear when the program is actually used. For example, if you fetch an API, everything may look fine at the declaration stage. But when you call that API, there are chances that it may return data or throw an error at runtime.
S, we should also know how to handle those errors.
Now, everyone has a basic idea of what errors are, so let's move ahead and see how to handle them.
Using try and catch blocks.
So basically, we use the try-catch block to handle errors.
In simple words, the try block tries to execute the code. If it is able to run the code successfully, then everything is fine. But if it fails to execute the code, it throws an error. That error is then passed to the catch block, and the catch block handles it based on how we want to return or show the error.
Now let's discuss both of them one by one. Once you understand the theory, then we will take a simple code example to see it as simple ways.
Try Block.
So basically, in the try block, you code runs. It tries to execute the code. If an error occurs on the very first line, it immediately passes that error to the catch block. It doesn't matter whether the error happens on the first line or the last line, if any error occurs, it directly sends it to the catch block.
But if no error occurs, the try block runs completely and gives the output, and the catch block is not executed.
So this is the basic idea of the try block.
Catch Block.
Now, the catch block has a simple job, it accepts whatever error comes from the try block and handles it. It shows or renders a message that an error has occurred. The error message is received as a parameter ( an error object ), and then it's depend on you how you want to send that error or display it to the users.
Note:- Its main job is just to handle the error and display it properly. No logic and code .
try {
let num = 10;
let result = num / 0;
console.log("Result:", result);
} catch (error) {
console.log("Something went wrong:", error.message);
}
This code is very simple. As you can see, if the code inside the try blocks runs successfully, then there is no need for the catch block. But if it does not run, then whatever error occurs will sent to the catch block.
The finally block.
So finally, what happens is that your code will definitely execute, whether the try blocks runs or the catch block runs . One of them will execute, and flow will continue.
Now, the
finallyblock gives you a kind of guarantee or promise that this part of the code will execute no matter what. From the word finally, you can understand that this code will run in the end.What happens is, if the code in try does not run and goes to catch, sometimes catch block may throw an error and terminate the program. But you may not want your program to just terminate like that, you may want to show a final message or give some final move, that's why we use the
finallykeyword.The
finallyblock always runs, whether an error occurs or not, and it is used to ensure that some final code is executed in every situation.
Throwing custom errors.
So in Custom errors.
One reason is that you don't always want every error to directly go to the catch block.
For example, suppose you are building a backend. In the backend, there are many validations. Now, you don't want that if even one validation fails, the entire try block should terminate and the error should go directly to catch.
To solve this problem, we use custom error methods.
If any
if conditionfails, you can return the error from that point itself. You can stop the execution instead of letting the whole try block fail and go to catch.Also, custom errors are not only used inside the try block, you can use them anywhere in your code because they are own defined errors.
So overall, the idea is that custom error gives more control.
Why error handling matters.
So now already have an idea of why we handle errors, but i will explain it at a better level. There are many examples, but i will give you two, one backend specific and one frontend specific.
First let's talk about the Backend.
Suppose you are building an API or fetching data from and API. In you input, you may pass correct data or wrong data. If the API gets what it expects, then it will work fine and return the result. But if the input data does not match the expectation, then the code will break. The API will not work, and you application may get stuck. Also, you might not even know where the error happened.
So whenever you build a backend, you should always expect errors for every value. You should think, if an error happens at runtime, how will i handle it, and how will i know where it occurred.
Now let's talk about the Frontend.
Your application is running, but you don't know who the user is, where they are coming from, or how they will use you app. A user can give unexpected input, or maybe they don't know how to use the software properly and enter wrong data. If wrong input is given, your application may crash because it was expecting something else and was not prepared for that error.
So what is the role of error handling here ?
If the user does something wrong or an error occurs, the application should not crash. Instead, we should already think about all possible cases, what a user can do and what kind of errors can happen. Based on that, when an error occurs, we should guide the user or show a suggestion like "don't do this" or "enter valid data."
Because of this, your application will not crash.
So that's why we need error handling.




