Sometimes you want to do something tricky with your good and you want your customers to have a very good user experience. And actually there is a small line that will determine whether or not your customers will be happy or sad. And of course, you want your customers to have a good experience, not a terrible one. Therefore, there is something called try and catch in JavaScript and many programming languages that will catch errors whenever it sees them. And whenever you are unsure of if that code that you are writing will produce some errors you need to use try and catch so that you catch errors and leave your customers happy. So let's now take some examples so that you understand much better.
So suppose that you are designing a login system and you need to be Get the username so that you let the user in to your system. And you have created a function called get name, okay. And this get name should return the user name or the name of that user so that you can leave them in. And you actually forgot to create this function at all, you just call this function without creating it. And in order to avoid the error, so if we just again, type this function name, get name. And now if I save and run, what I'm gonna get, obviously is an error.
And we don't want the user to see that, because that will absolutely will make our system so weak and the user will have a terrible user experience and they will leave your program and go to another one. In order to fix that in order to avoid this error. We use something called try and catch. So you would cry and then curly braces, and you add your code that you are afraid if it will produce an error or not, you are you are not sure if it's gonna produce some errors. So here you just add, you just surround them with a try and error with a curly braces. And then at the end of that queue, closing curly braces, you type catch, and you add parentheses.
And inside these parentheses, you add just the word e or air or anything and JavaScript will know that this is the air that it's going to catch. And then you open another curly braces and inside them, you use for example, the console dot log, and you print that error to the user. Right? Now, if I save on run, guess what, what's gonna happen? What's gonna happen instead, is that it's gonna tell you that gate name is not defined. It's going to tell you reference error.
This is not a collapse, or this is not a break in our system. It's just catching an error. JavaScript has catched, the error, the fact that this function has not been defined, and it just printed out to the user. But sometimes you want to be more specific with your user, you want to display the error and tell the user that look, I have an error, that it's bla bla bla, for example, you have forgotten to type in your name or something like that, or the name is not defined, you can specifically tell the user that the error is a blah, blah, blah. So here we are going to do that. For example, suppose that you have a variable called name, var name.
And this variable is going to store the user name or the name of that user, so that you firstly need to check if it exists or not, if it exists, or if it's different. We are going to let the user and if it's not defined, we are going to prevent the user from logging in. And we are going to display the error semantically and clearly to the user without our system being broken. So here, what I'm going to do is I'm going to use your if statement. And I'm going to use this name, and then triple equal sign, and then undefined. And then what that means if our name, this one, or the name of the user that the user is going to type in our system is undefined if it's not defined, instead of displaying just a normal error, a general error, we are going to be more specific with our user.
Therefore we are going to use a keyword called throw, here throw. And what it does is that it makes your error more specific It also prevents you from from continue with your code. So once there is an error, the complete code will not be executed, it's it's going to stop where it is. And it's going to display the error. And it's not going to go further with your code. So you just type throw, and then the error that you want to display here, for example, sorry, sorry, your name.
Your name is not defined, or whatever you want. It's just a cast a custom message for the user. Now, if I save on run, as you can see what I what I'm going to get is sorry, Your name is not defined because this name is not defined. Because the user, for example, might have forgotten to type in their name and they clicked on the login button without clicking without typing in their name. They just accidentally maybe accidentally or intentionally intentionally clicked on the login without filling in their name and password. So, here you just be you you are just being more specific with your user and you are telling them that sorry, there is an error sorry, Your name is not defined, or whatever message you want to send to your user instead of sending a general message.
So in conclusion, if you are not sure if your code will produce some errors, you just surround it with try and catch and catch will handle the error will display the error systematically and clearly to the user without any terrible user experience. If you have a specific message that you want to send to the user, in case there is an error, you just use the throw keyword